[Tutor] List comprehension question

Steven D'Aprano steve at pearwood.info
Mon Nov 8 23:17:48 CET 2010


Stefan Behnel wrote:
> Hugo Arts, 08.11.2010 00:53:
[...]
>> On another note, getting rid of the list comprehension and using a
>> generator expression will be even faster, since you won't have to
>> build the list.
> 
> I gave this suggestion a try. It is true for me when run in CPython 3.2:
[...]
> However, it is no longer true when I run the same thing in Cython:
[...]
> Lesson learned:
> 
> Sometimes, it can be worth splitting a loop in two, one with a tight 
> filter for the values, and another one to work on the remaining values.

Nice analysis, thank you. However, surely the most important lesson to 
be learned is:

Don't make assumptions about what is faster, measure it and find out!



-- 
Steven


More information about the Tutor mailing list