[Tutor] replacing a loop

Mats Wichmann mats at wichmann.us
Mon Jun 24 13:48:39 EDT 2019


On 6/24/19 10:15 AM, johnf wrote:
.
> 
> Since I use a lot of similar loops to populate many dropdown controls I
> started investigating the use of list comprehensions.  But I can't
> figure out how to use them in this loop and wonder if it will improve
> the performance. 

To amplify a tiny bit on what Peter said: comprehensions are a more
concise way to express a loop-to-build-a-collection, but I don't believe
were intended specifically as a performance improvement.

They may be a little faster on the principle of "avoiding dots" - that
is, the lookup every time through your loop from listname.append does
take a little bit of time.  And probably more significantly, append is a
function that has to be called, and the setup stack frame/call/return
sequence also takes some time. So depending on circumstances you may see
insignificant or significant performance differences.

But, again -

Rule number one: only optimize when there is a proven speed bottleneck



More information about the Tutor mailing list