[Python-Dev] Faster list comprehensions

Collin Winter collinw at gmail.com
Fri Mar 3 13:58:03 CET 2006

Hello all,

I've just posted a patch to SF (python.org/sf/1442442) that I thought
I'd bring to everyone's attention. Executive summary: by changing the
behaviour of the unused LIST_APPEND opcode, it's possible to get a 16%
speed boost for list comprehensions.

Currently, list comprehensions are built up by calling a bound method
on the storage list. This is pretty inefficient, as it requires extra
stack manipulation and a function call for each element in the final
list. My patch takes the LIST_APPEND opcode (which existed in the eval
loop but not the compiler) and changes its behaviour; rather than take
its list argument off the stack, it takes a stack offset, allowing it
to leave the list object in place. This also replaces the slow Python
function call with a fast call directly to PyList_Append. In total,
this results in a 16% speed improvement [1].

[1] Benchmarking was done by taking the list comprehension examples in
Lib/test/test_grammar.py and running them in a loop a few thousand
times. Python revision 42780 took 1.69 seconds to run the examples
10000 times; the updated version takes 1.41 seconds.

Collin Winter

More information about the Python-Dev mailing list