[Tutor] replacing a loop

johnf johnf at jfcomputer.com
Mon Jun 24 16:50:36 EDT 2019


Actually I do not see a reply from Peter??????  I don't have a clue what 
was said.

I realize that performance is not a big issue in this case - of course 
an increase in speed is always welcome.  I was more interested in a 
better understanding of the list comprehensions.  Since I have so many 
of these loops I thought it might be best if I tried using comprehensions.


Johnf

On 6/24/19 10:48 AM, Mats Wichmann wrote:
> 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
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor


More information about the Tutor mailing list