string goes away

Michael Chermside mcherm at mcherm.com
Fri Apr 1 14:37:53 EST 2005


Andreas Beyer writes:
> 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 Beyer writes:
> 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




More information about the Python-list mailing list