Skip to content

Instantly share code, notes, and snippets.

@HugoArts
Created April 23, 2010 16:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HugoArts/376767 to your computer and use it in GitHub Desktop.
Save HugoArts/376767 to your computer and use it in GitHub Desktop.
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