

Efficiency of list comprehensions - llambda
http://blog.cdleary.com/2010/04/efficiency-of-list-comprehensions/

======
koenigdavidmj
1\. You should be profiling. If this is your bottleneck, then consider tricks
like this, or write that loop in C. This is probably not your bottleneck.

2\. This is very interpreter-dependent. Any JIT worth its salt (like, say,
PyPy) would probably decide to inline the append code if we're in this loop
long enough for it to matter.

------
jessaustin
Interesting. It seems like he also might have wanted to try a generator
expression for the "throw away the list" case. You can always wrap a generator
expression with max() or any() or something similar to trigger the iteration.
You'd at least be avoiding the "LIST_APPEND" bytecode. With the additional
function call I'm not sure the generator expression would be better, but I
must admit that in my own code I'm pretty anal about converting all list comps
to gen exps that I possibly can.

~~~
talaketu
"any" short circuits - you probably are not throwing away the boolean result.

I do agree with the author that using a comprehension for the side-effect is
smelly.

~~~
adamtj
But "any" short-circuits: that smells the same as a list comprehension for
side effects.

The beauty of Python is that the one, obvious way to do something is usually
the right way. When you try to force something like this, you either run into
silly bugs like "any" short-circuiting, or you waste your limited brainpower
trying to avoid them. You're usually better off looking for algorithmic or
architectural optimizations for speed, or refactoring for readability.

~~~
talaketu
"any" applied to a generator expression with side-effects is smelly because of
the generator expression with side-effects, not because of the short-circuit.
"any" applied to a pure generator expression expresses the algorithmic
optimization known as short circuiting.

Once you are at the point of considering execution time to be a side effect,
the purity of any non-trivial expression vanishes.

