[Python-Dev] Re: Prospective Peephole Transformation

Fredrik Lundh fredrik at pythonware.com
Fri Feb 18 16:45:32 CET 2005


Phillip J. Eby wrote:

>>Only contains expressions are translated:
>>
>>     "if x in [1,2,3]"
>>
>>currently turns into:
>>
>>     "if x in (1,2,3)"
>>
>>and I'm proposing that it go one step further:
>>
>>     "if x in Seachset([1,2,3])"
>
> ISTM that whenever I use a constant in-list like that, it's almost always with just a few (<4) 
> items, so it doesn't seem worth the extra effort (especially disrupting the marshal module) just 
> to squeeze out those extra two comparisons and replace them with a hashing operation.

it could be worth expanding them to

    "if x == 1 or x == 2 or x == 3:"

though...

C:\>timeit -s "a = 1" "if a in (1, 2, 3): pass"
10000000 loops, best of 3: 0.11 usec per loop
C:\>timeit -s "a = 1" "if a == 1 or a == 2 or a == 3: pass"
10000000 loops, best of 3: 0.0691 usec per loop

C:\>timeit -s "a = 2" "if a == 1 or a == 2 or a == 3: pass"
10000000 loops, best of 3: 0.123 usec per loop
C:\>timeit -s "a = 2" "if a in (1, 2, 3): pass"
10000000 loops, best of 3: 0.143 usec per loop

C:\>timeit -s "a = 3" "if a == 1 or a == 2 or a == 3: pass"
10000000 loops, best of 3: 0.187 usec per loop
C:\>timeit -s "a = 3" "if a in (1, 2, 3): pass"
1000000 loops, best of 3: 0.197 usec per loop

C:\>timeit -s "a = 4" "if a in (1, 2, 3): pass"
1000000 loops, best of 3: 0.225 usec per loop
C:\>timeit -s "a = 4" "if a == 1 or a == 2 or a == 3: pass"
10000000 loops, best of 3: 0.161 usec per loop

</F> 





More information about the Python-Dev mailing list