Created
April 23, 2010 16:33
-
-
Save HugoArts/376767 to your computer and use it in GitHub Desktop.
This file contains 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
import timeit | |
import bisect | |
r = 10000 | |
s = "a = range(%s);" % r | |
print('starting timing...') | |
a = timeit.Timer(setup=s, stmt="55555 in a") | |
print('timing list: %s' % a.repeat(3, r)) | |
f = timeit.Timer(setup=s + 'from bisect import bisect', stmt="a[bisect(a, 55555) - 1] == 55555") | |
print('timing bisect: %s' % f.repeat(3, r)) | |
b = timeit.Timer(setup=s, stmt="55555 in frozenset(a)") | |
print('timing frozenset (one lookup): %s' % b.repeat(3, r)) | |
c = timeit.Timer(setup=s, stmt="55555 in dict(zip(a,a))") | |
print('timing dict (one lookup)": %s' % c.repeat(3, r)) | |
d = timeit.Timer(setup=s, stmt="d = frozenset(a)\nfor x in a:\n\t55555 in d") | |
print('timing frozenset (many lookups): %s' % d.repeat(3, 1)) | |
e = timeit.Timer(setup=s, stmt="d = dict(zip(a,a))\nfor x in a:\n\t55555 in d") | |
print('timing dict (many lookups): %s' % e.repeat(3, 1)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment