Problem of function calls from map()

Steve Holden steve at holdenweb.com
Wed Aug 23 19:39:35 EDT 2006


Fredrik Lundh wrote:
> 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>
> 
Well I guess if people wanted to argue for keeping the functionals this 
should be on the list ...

regards
  Steve
-- 
Steve Holden       +44 150 684 7255  +1 800 494 3119
Holden Web LLC/Ltd          http://www.holdenweb.com
Skype: holdenweb       http://holdenweb.blogspot.com
Recent Ramblings     http://del.icio.us/steve.holden




More information about the Python-list mailing list