Last active
December 21, 2017 01:01
-
-
Save davidwtbuxton/1ddadf75bdc1779e5be72a4cffb6e1a1 to your computer and use it in GitHub Desktop.
Testing a variable matches 1 of 2 values (Python 2.7)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ python -m timeit -s "v = 'foo'" "v == 'foo' or v == 'bar'" | |
10000000 loops, best of 3: 0.163 usec per loop | |
$ python -m timeit -s "v = 'bar'" "v == 'foo' or v == 'bar'" | |
1000000 loops, best of 3: 0.233 usec per loop | |
$ python -m timeit -s "v = 'baz'" "v == 'foo' or v == 'bar'" | |
1000000 loops, best of 3: 0.241 usec per loop | |
$ python -m timeit -s "v = 'foo'" "v in ('foo', 'bar')" | |
10000000 loops, best of 3: 0.145 usec per loop | |
$ python -m timeit -s "v = 'bar'" "v in ('foo', 'bar')" | |
10000000 loops, best of 3: 0.155 usec per loop | |
$ python -m timeit -s "v = 'baz'" "v in ('foo', 'bar')" | |
10000000 loops, best of 3: 0.166 usec per loop | |
$ python -m timeit -s "v = 'foo'" "v in {'foo', 'bar'}" | |
1000000 loops, best of 3: 0.232 usec per loop | |
$ python -m timeit -s "v = 'bar'" "v in {'foo', 'bar'}" | |
1000000 loops, best of 3: 0.236 usec per loop | |
$ python -m timeit -s "v = 'baz'" "v in {'foo', 'bar'}" | |
1000000 loops, best of 3: 0.238 usec per loop | |
$ python --version | |
Python 2.7.14 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment