string goes away

"Martin v. Löwis" martin at v.loewis.de
Sun Apr 3 10:21:09 EDT 2005


Andreas Beyer wrote:
> Yeeh, I was expecting something like that. The only reason to use map() 
> at all is for improving the performance.
> That is lost when using list comprehensions (as far as I know). So, this 
> is *no* option for larger jobs.

Don't believe anything you hear right away, especially not when it comes
to performance, especially not wrt. Python.

martin at mira:~$ python -m timeit -s "items=['']*10000;import string" 
"map(string.upper, items)"
100 loops, best of 3: 6.32 msec per loop
martin at mira:~$ python -m timeit -s "items=['']*10000;import string" 
"[s.upper for s in items]"
100 loops, best of 3: 2.22 msec per loop

So using map is *no* option for larger jobs.

Of course that statement is also false. Performance prediction is very
difficult, and you cannot imply much from this benchmark. In other
cases, list comprehension may be slower than map. More likely, for real
(i.e. non-empty) strings, the cost of .upper will make the precise
implementation of the loop irrelevant for performance reasons.

Regards,
Martin



More information about the Python-list mailing list