lint warnings

Steven D'Aprano steve+comp.lang.python at pearwood.info
Tue Feb 15 22:25:53 CET 2011


On Tue, 15 Feb 2011 14:32:13 +0000, Duncan Booth wrote:

> <snip>
>> Also, as already shown, the map version is faster.
> 
> In most cases the list comprehension is faster. Try timing it.

For an extremely specialised and minimal definition of "most cases".


> C:\Python27>python.exe lib\timeit.py -s "def double(x): return x*2" -s
> "data=range(10000)" "map(double, data)" 1000 loops, best of 3: 1.82 msec
> per loop
> 
> C:\Python27>python.exe lib\timeit.py -s "def double(x): return x*2" -s
> "data=range(10000)" "[x*2 for x in data]" 1000 loops, best of 3: 879
> usec per loop

You're not comparing apples and apples. Try calling the function double() 
from the list comp, instead of inlining it.

Yes, if you can avoid a function call, list comps are faster than map. 
That's a valuable micro-optimization, although the warning about 
premature optimizations and small efficiencies apply. But the number of 
operations that can be inlined is only a vanishingly small subset of all 
operations you might choose to perform, and in general, map is faster 
than list comps with both C built-ins and pure Python functions.

There's no doubt that list comps are a moderate win for readability and 
speed when you can inline the function as opposed to defining a lambda 
inside the map. But otherwise, it's a myth that map is generally slower 
and longer than list comps. It's usually the other way around:

map(function, data)
[function(x) for x in data]

Even in Python3 where map becomes an iterator, it's still shorter:

list(map(function, data))
[function(x) for x in data]

(by two whole characters! Woo hoo, I don't need to upgrade my hard 
drive!!! *wink*)

Conceptually, a map is a single operation. You don't have to think about 
the implementation, namely, "iterate over the sequence, extracting each 
item into a temporary variable, and call the function on that temporary 
variable". You just map the function to the sequence, and don't worry 
about the implementation. I really appreciate that. Sometimes I wish that 
Python would let me write function(data), although given that this would 
be ambiguous, map is the next best thing.


> map is only likely to be faster if you wanted to call a function in both
> cases. If you have an expression that can be inlined you save the
> function call overhead with the list comprehension.

Exactly.


-- 
Steven



More information about the Python-list mailing list