Problem of function calls from map()

Fredrik Lundh fredrik at pythonware.com
Tue Aug 22 14:26:24 EDT 2006


Sion Arrowsmith wrote:

>> (you cannot really use "profile" to *benchmark* things written in Python either; the
>> profiler tells you where a given program spends the time, not how fast it is in com-
>> parision with other programs)
> 
> Hmm. Playing around with timeit suggests that although split() *is*
> faster than split("\t"), it's fractional, rather than the OP's four
> times faster. Is the overhead of profile keeping track of calls in
> Python getting in the way?

correct.

> And why can map() keep everything at the C level when the list com-
 > prehension can't?

map is called with two Python objects (the str.split callable and the 
sequence object), while the list comprehension is turned into a byte-
code loop that evaluates s.split for each item in the sequence; compare 
and contrast:

 >>> def func(a):
...     return map(str.split, a)
...
 >>> dis.dis(func)
   2           0 LOAD_GLOBAL              0 (map)
               3 LOAD_GLOBAL              1 (str)
               6 LOAD_ATTR                2 (split)
               9 LOAD_FAST                0 (a)
              12 CALL_FUNCTION            2
              15 RETURN_VALUE

 >>> def func(a):
...     return [s.split() for s in a]
...
 >>> dis.dis(func)
   2           0 BUILD_LIST               0
               3 DUP_TOP
               4 STORE_FAST               1 (_[1])
               7 LOAD_FAST                0 (a)
              10 GET_ITER
         >>   11 FOR_ITER                19 (to 33)
              14 STORE_FAST               2 (s)
              17 LOAD_FAST                1 (_[1])
              20 LOAD_FAST                2 (s)
              23 LOAD_ATTR                0 (split)
              26 CALL_FUNCTION            0
              29 LIST_APPEND
              30 JUMP_ABSOLUTE           11
         >>   33 DELETE_FAST              1 (_[1])
              36 RETURN_VALUE

(LOAD_GLOBAL and LOAD_ATTR does full name lookups, while LOAD_FAST loads 
a local variable using an integer index)

</F>




More information about the Python-list mailing list