[Python-Dev] Don't set local variable in a list comprehension or generator
Nick Coghlan
ncoghlan at gmail.com
Thu May 19 11:03:10 CEST 2011
On Thu, May 19, 2011 at 7:34 AM, Victor Stinner
<victor.stinner at haypocalc.com> wrote:
> But it is slower whereas I read somewhere than generators are faster
> than loops.
Are you sure it wasn't that generator expressions can be faster than
list comprehensions (if the memory savings are significant)?
Or that a reduction function with a generator expression can be faster
than a module-level explicit loop (due to the replacement of
dict-based variable assignment with fast locals in the generator and C
looping in the reduction function)?
In general, as long as both are using fast locals and looping in
Python, I would expect inline looping code to be faster than the
equivalent generator (but often harder to maintain due to lack of
reusability).
Cheers,
Nick.
--
Nick Coghlan | ncoghlan at gmail.com | Brisbane, Australia
More information about the Python-Dev
mailing list