Why [] () faster than the list?

I recently compared [] and list () processing speed, and was surprised to find [] running speed ratio list () three times faster or more. I ran the same test with} and {dict (), the results are almost identical: [] {} and spent about two 0.128sec / million, while the list () and dict () spent about each 0.428sec / million times.

Later, I checked the investigation reasons, the conclusion is as follows:

list () requires a global look and function calls, but [] compiled into a single instruction.

 

Python 2.7.3
>>> import dis
>>> print dis.dis(lambda: list())
  1           0 LOAD_GLOBAL              0 (list)
              3 CALL_FUNCTION            0
              6 RETURN_VALUE        
None
>>> print dis.dis(lambda: [])
  1           0 BUILD_LIST               0
              3 RETURN_VALUE        
None

                                                                 basic data types python

 

Guess you like

Origin www.cnblogs.com/pythongood/p/11108579.html