Python list() vs list comprehension building speed

£可爱£侵袭症+ 提交于 2019-11-28 10:50:54

The list comprehension executes the loop in Python bytecode, just like a regular for loop.

The list() call iterates entirely in C code, which is far faster.

The bytecode for the list comprehension looks like this:

>>> import dis
>>> dis.dis(compile("[x for x in xrange(1000000)]", '<stdin>', 'exec'))
  1           0 BUILD_LIST               0
              3 LOAD_NAME                0 (xrange)
              6 LOAD_CONST               0 (1000000)
              9 CALL_FUNCTION            1
             12 GET_ITER            
        >>   13 FOR_ITER                12 (to 28)
             16 STORE_NAME               1 (x)
             19 LOAD_NAME                1 (x)
             22 LIST_APPEND              2
             25 JUMP_ABSOLUTE           13
        >>   28 POP_TOP             
             29 LOAD_CONST               1 (None)
             32 RETURN_VALUE        

The >> pointers roughly give you the boundaries of the loop being executed, so you have 1 million STORE_NAME, LOAD_NAME and LIST_APPEND steps to execute in the Python bytecode evaluation loop.

list() on the other hand just grabs the values from the xrange() iterable directly by using the C API for object iteration, and it can use the length of the xrange() object to pre-allocate the list object rather than grow it dynamically.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!