[Tutor] List comprehension question

Alan Gauld alan.gauld at btinternet.com
Mon Nov 8 19:34:13 CET 2010


"Stefan Behnel" <stefan_ml at behnel.de> wrote

>> 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.

And another good example of how hard it is to guess the optimal
solution in performance terms. It is one area where nothing beats
trial and test. And another reason why premature optimisation should
be resisted! :-)

Alan G. 




More information about the Tutor mailing list