[PATCH] cleanup: use x in (a, b) instead of x == a or x == b

Isaac Jurado diptongo at gmail.com
Thu Sep 23 12:02:18 CDT 2010


Replying Matt Mackall:
> 
> optimal:
> $ python -m timeit -s 'a, b, c = 0, 0, 2' -c 'if a == b or a == c: pass'
> 10000000 loops, best of 3: 0.083 usec per loop
> $ python -m timeit -s 'a, b, c = 0, 0, 2' -c 'if a == (b, c): pass'
> 1000000 loops, best of 3: 0.28 usec per loop
> 
> pessimal:
> $ python -m timeit -s 'a, b, c = 0, 1, 2' -c 'if a == b or a == c: pass'
> 10000000 loops, best of 3: 0.134 usec per loop
> $ python -m timeit -s 'a, b, c = 0, 1, 2' -c 'if a == (b, c): pass'
> 1000000 loops, best of 3: 0.29 usec per loop

Just for the record.  I don't know if the "a == (b, c)" expression is
intentional or a typo, but in both pessimal and optimal cases there is
almost no time difference because a scalar is being compared with a
tuple.

Changing it to "a in (b, c)", what the whole thread was all about, shows
the difference between the two cases.

I just commented it if someone arrives to this thread in the future
(from the mailing list archives), as the mercurial-devel list is
bringing up many interesting Python microbenchmark discussions.

Cheers.

-- 
Isaac Jurado

"The noblest pleasure is the joy of understanding."
                                  Leonardo da Vinci


More information about the Mercurial-devel mailing list