shouldn't list comprehension be faster than for loops?

Ryan Kelly ryan at rfk.id.au
Fri Dec 18 20:28:32 EST 2009


> > Tenting the time spent by each approach (using time.clock()), with a
> > file with about 100,000 entries, I get 0.03s for the loop and 0.05s
> > for the listcomp.
>
> Anything else being equal, list comprehensions will be the faster
> becuase they incur fewer name and attribute lookups. It will be the
> same as the difference between a for loop and a call to map. A list
> comprehension is basically an enhancement of map.

Not so.  If you use the "dis" module to peek at the bytecode generated
for a list comprehension, you'll see it's very similar to that generated
for an explicit for-loop.  The byte-code for a call to map is very
different.

Basically:  both a for-loop and a list-comp do the looping in python
bytecode, while a call to map will do the actual looping in C.

>>> def comper():
...     return [i*2 for i in xrange(10)]
... 
>>> 
>>> dis.dis(comper)
  2           0 BUILD_LIST               0
              3 DUP_TOP             
              4 STORE_FAST               0 (_[1])
              7 LOAD_GLOBAL              0 (xrange)
             10 LOAD_CONST               1 (10)
             13 CALL_FUNCTION            1
             16 GET_ITER            
        >>   17 FOR_ITER                17 (to 37)
             20 STORE_FAST               1 (i)
             23 LOAD_FAST                0 (_[1])
             26 LOAD_FAST                1 (i)
             29 LOAD_CONST               2 (2)
             32 BINARY_MULTIPLY     
             33 LIST_APPEND         
             34 JUMP_ABSOLUTE           17
        >>   37 DELETE_FAST              0 (_[1])
             40 RETURN_VALUE        
>>>
>>>
>>>
>>> def maper():
...     return map(lambda i: i*2,xrange(10))
... 
>>> dis.dis(maper)
  2           0 LOAD_GLOBAL              0 (map)
              3 LOAD_CONST               1 (<code object ...)
              6 MAKE_FUNCTION            0
              9 LOAD_GLOBAL              1 (xrange)
             12 LOAD_CONST               2 (10)
             15 CALL_FUNCTION            1
             18 CALL_FUNCTION            2
             21 RETURN_VALUE        
>>> 



  Cheers,

    Ryan

-- 
Ryan Kelly
http://www.rfk.id.au  |  This message is digitally signed. Please visit
ryan at rfk.id.au        |  http://www.rfk.id.au/ramblings/gpg/ for details

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 197 bytes
Desc: This is a digitally signed message part
URL: <http://mail.python.org/pipermail/python-list/attachments/20091219/32b992f8/attachment-0001.sig>


More information about the Python-list mailing list