List comprehensions performance

Alex Martelli aleaxit at yahoo.com
Thu Sep 30 04:48:37 EDT 2004


Neuruss <luismg at gmx.net> wrote:
   ...
> What I'd like to know is if using list comprehensions would give me a
> performance advantage over traditional for loops or not.

Probably, a little, depending on the exact Python release you're using,
and on exactly what you're doing -- as others have suggested, timeit.py
can help.  Don't expect _dramatic_ differences.

> I'm getting fond of list comprehensions, but I wonder if it's wise to
> abuse of them...

No, by definition of "abuse".  Use list comprehensions to make lists,
not instead of perfectly normal loops.  Consider for example:

kallisti:~/cb alex$ python2.4 timeit.py -s'def f():pass' 'for x in
xrange(999): f()'
1000 loops, best of 3: 1.25e+03 usec per loop
kallisti:~/cb alex$ python2.4 timeit.py -s'def f():pass' 'for x in
xrange(999): f()'
1000 loops, best of 3: 1.29e+03 usec per loop
kallisti:~/cb alex$ python2.4 timeit.py -s'def f():pass' '[f() for x in
xrange(999)]'
1000 loops, best of 3: 1.45e+03 usec per loop
kallisti:~/cb alex$ python2.4 timeit.py -s'def f():pass' '[f() for x in
xrange(999)]'
1000 loops, best of 3: 1.44e+03 usec per loop

So, in this case, abusing list comprehensions slows you down by over
10%.

More generally, _who cares_ about this kind of speedups?!  Premature
optimization is the root of all evil in programming.  Make your programs
clear, simple, readable -- that's ALWAYS important!  So is using general
algorithms and data structures with good O() characteristics if the
input size is liable to grow a lot.  But squeezing out 10% or 20% here
or there is going to matter in a TINY minority of cases.  Don't distort
your coding for such purposes!


Alex



More information about the Python-list mailing list