str performance in python

后端 未结 2 1596
暖寄归人
暖寄归人 2021-01-30 04:54

While profiling a piece of python code (python 2.6 up to 3.2), I discovered that the str method to convert an object (in my case an intege

2条回答
  •  一向
    一向 (楼主)
    2021-01-30 05:27

    '%s' % 100000 is evaluated by the compiler and is equivalent to a constant at run-time.

    >>> import dis
    >>> dis.dis(lambda: str(100000))
      8           0 LOAD_GLOBAL              0 (str)
                  3 LOAD_CONST               1 (100000)
                  6 CALL_FUNCTION            1
                  9 RETURN_VALUE        
    >>> dis.dis(lambda: '%s' % 100000)
      9           0 LOAD_CONST               3 ('100000')
                  3 RETURN_VALUE        
    

    % with a run-time expression is not (significantly) faster than str:

    >>> Timer('str(x)', 'x=100').timeit()
    0.25641703605651855
    >>> Timer('"%s" % x', 'x=100').timeit()
    0.2169809341430664
    

    Do note that str is still slightly slower, as @DietrichEpp said, this is because str involves lookup and function call operations, while % compiles to a single immediate bytecode:

    >>> dis.dis(lambda x: str(x))
      9           0 LOAD_GLOBAL              0 (str)
                  3 LOAD_FAST                0 (x)
                  6 CALL_FUNCTION            1
                  9 RETURN_VALUE        
    >>> dis.dis(lambda x: '%s' % x)
     10           0 LOAD_CONST               1 ('%s')
                  3 LOAD_FAST                0 (x)
                  6 BINARY_MODULO       
                  7 RETURN_VALUE        
    

    Of course the above is true for the system I tested on (CPython 2.7); other implementations may differ.

提交回复
热议问题