List comprehensions performance

Alex Martelli aleaxit at yahoo.com
Thu Sep 30 04:55:52 EDT 2004


Raymond Hettinger <vze4rx4y at verizon.net> wrote:

> [Neuruss] What I'd like to know is if using list comprehensions would give
> me a > performance advantage over traditional for loops or not.
> 
> For Py2.4, list comprehensions are much faster than equivalent for-loops.

...but if the for loop is NOT equivalent (it doesn't accumulate results
into a resulting list), it's still faster.  As I posted:

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

the LC is paying for the building of a list of 999 references to None,
which the for loop easily avoids, so the for loop is much faster here.

Of course, it WAS way more pronounced in 2.3:

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


> > I'm getting fond of list comprehensions, but I wonder if it's wise to
> > abuse of them...
> 
> Use whatever is clearest.
> Don't abuse anything.

Absolutely.  And: use 2.4 -- note that the SLOWER solution in 2.4 still
gains a good 20% over the FASTER one in 2.3... anybody who cares about
performance should be giving 2.4 an intense workout already, rarin' for
the day in which it will be officially released so it can be sensibly
used in customer-delivered software... (and, for those not in the know,
I should point out that Raymond was the one most responsible for the
overall impressive speed-up 2.2->2.3->2.4...!!!-).


Alex



More information about the Python-list mailing list