[BangPypers] Are comprehensions faster?

Anand Balachandran Pillai abpillai at gmail.com
Mon Feb 9 15:37:51 CET 2009

On Mon, Feb 9, 2009 at 7:46 PM, Venkatraman S <venkat83 at gmail.com> wrote:
> On Mon, Feb 9, 2009 at 7:11 PM, Senthil Kumaran <orsenthil at gmail.com> wrote:
>> >
>> > You see that is slow and the reason is  l.append being called again
>> > and again. lets eliminate it.
>> Sorry, it is slow because the append method "Look-up" on the list
>> object is done again and again.
> Both LC and For-loops generate the same bytecodes!

More than any performance increase, comprehensions are a much
neater and Pythonic way of doing things. A list comprehension for
any non-trivial work will often perform much better than a similar
map/reduce/lambda solution because it is optimized to produce

# List comp version of squaring the list
>>> timeit.Timer("l=range(1000);[x*x for x in l]").timeit(number=10000)
# Map version with lambda
>>> timeit.Timer("l=range(1000);map(lambda x:x*x, l)").timeit(number=10000)

Part of this is because function calls are one of the costliest of
Python operations. List comps often inline the code of function calls
in an expression, which reduces the cost.

You can use it as a rule of thumb to decide whether to
choose a list comp or an equivalent functional solution (map, filter or a
regular function) - i.e if the function can be inlined in a list comprehension
by removing all function calls into an expression, the list comp will
be faster 99% of the time. However if every function can't be inlined,
then the list comp might be not as fast or even slower, so it requires
testing to choose the solution.

And if you are operating on large lists and memory is an issue,
*never* use list comps since they are "greedy" and produce the list
in one shot. In that case go for an equivalent generator expression
or generator function.

> -V-
> http://twitter.com/venkat83
> _______________________________________________
> BangPypers mailing list
> BangPypers at python.org
> http://mail.python.org/mailman/listinfo/bangpypers


More information about the BangPypers mailing list