This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author serhiy.storchaka
Recipients benjamin.peterson, brett.cannon, ncoghlan, serhiy.storchaka, yselivanov
Date 2018-02-23.21:44:18
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1519422258.29.0.467229070634.issue32925@psf.upfronthosting.co.za>
In-reply-to
Content
Currently a list of constants is replaced with constant tuple in `x in [1, 2]` and `for x in [1, 2]`. The resulted AST is the same as for `x in (1, 2)` and `for x in (1, 2)`.

The proposed simple PR extends this optimization to lists containing non-constants. `x in [a, b]` will be changed into `x in (a, b)` and `for x in [a, b]` will be changed into `for x in (a, b)`. Since creating a tuple is faster than creating a list the latter form is a tiny bit faster.

$ ./python -m timeit -s 'a, b = 1, 2' -- 'for x in [a, b]: pass'
5000000 loops, best of 5: 93.6 nsec per loop

$ ./python -m timeit -s 'a, b = 1, 2' -- 'for x in (a, b): pass'
5000000 loops, best of 5: 74.3 nsec per loop

./python -m timeit -s 'a, b = 1, 2' -- '1 in [a, b]'
5000000 loops, best of 5: 58.9 nsec per loop

$ ./python -m timeit -s 'a, b = 1, 2' -- '1 in (a, b)'
10000000 loops, best of 5: 39.3 nsec per loop
History
Date User Action Args
2018-02-23 21:44:18serhiy.storchakasetrecipients: + serhiy.storchaka, brett.cannon, ncoghlan, benjamin.peterson, yselivanov
2018-02-23 21:44:18serhiy.storchakasetmessageid: <1519422258.29.0.467229070634.issue32925@psf.upfronthosting.co.za>
2018-02-23 21:44:18serhiy.storchakalinkissue32925 messages
2018-02-23 21:44:18serhiy.storchakacreate