M
Michael Chermside
Andreas said: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.
Skip Montanaro replied:
Did you test your hypothesis?
% python -m timeit -s 'lst = ["abc"*10]*1000 ; import string' 'map(string.upper, lst)'
100 loops, best of 3: 3.72 msec per loop
% python -m timeit -s 'lst = ["abc"*10]*1000' '[s.upper() for s in lst]'
100 loops, best of 3: 2.23 msec per loop
Andreas said:OK, you won. I read in an (regretably old) guidline for improving
Python's performance that you should prefer map() compared to list
comprehensions. Apparently the performance of list comprehensions has
improved a lot, which is great.
If the lesson you learned here was that list comprehensions are at
least as fast as list comprehensions, then you've learned the wrong
lesson.
The REAL lesson here is that you shouldn't follow any "optimization"
rules without actually testing them. If you don't have time to test,
then just don't optimize... write whatever is most readable. If you
NEED more speed, then profiling and testing will show you what to
fix. (Using a better algorithm is a different story... do that
whenever you need it.)
-- Michael Chermside