Author serhiy.storchaka
Recipients benjamin.peterson, brett.cannon, ncoghlan, serhiy.storchaka, steven.daprano, yselivanov
Date 2018-02-16.17:31:57
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1518802317.94.0.467229070634.issue32856@psf.upfronthosting.co.za>
In-reply-to
Content
Here are some microbenchmarks. But since this code always is a part of complex expression it may be not make sense to talk about its pure speed.

$ ./python -m timeit -s 'a = list(range(1000))' -- '[y for x in a for y in [x]]'
Unpatched:  5000 loops, best of 5: 81.4 usec per loop
Patched:   10000 loops, best of 5: 19.8 usec per loop

For comparison the variant without temporary variable:

$ ./python -m timeit -s 'a = list(range(1000))' -- '[x for x in a]'
20000 loops, best of 5: 15.6 usec per loop

The overhead of using temporary variable is decreased from 66 usec to 4.2 usec (by 16 times).

In more realistic examples the subexpression assigned to temporary variable is slow. Otherwise it would be not worth to use a temporary variable. Therefore the relative speed up of the whole comprehension expression caused by this optimization is much smaller.
History
Date User Action Args
2018-02-16 17:31:58serhiy.storchakasetrecipients: + serhiy.storchaka, brett.cannon, ncoghlan, benjamin.peterson, steven.daprano, yselivanov
2018-02-16 17:31:57serhiy.storchakasetmessageid: <1518802317.94.0.467229070634.issue32856@psf.upfronthosting.co.za>
2018-02-16 17:31:57serhiy.storchakalinkissue32856 messages
2018-02-16 17:31:57serhiy.storchakacreate