Skip to content

Instantly share code, notes, and snippets.

@yosemitebandit
Created December 2, 2014 21:00
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 yosemitebandit/5c5e4fe2817895a25f5f to your computer and use it in GitHub Desktop.
Save yosemitebandit/5c5e4fe2817895a25f5f to your computer and use it in GitHub Desktop.
using timeit
# via
# https://mail.python.org/pipermail/python-list/2010-August/586080.html
import random
import timeit
with open('/usr/share/dict/words') as infile:
words = [line.strip() for line in infile]
sample = random.sample(words, 501)
n = sample.pop()
y = random.choice(sample)
d = dict.fromkeys(sample)
s = set(sample)
f = frozenset(sample)
l = list(sample)
t = tuple(sample)
for lookup in d, s, f, l, t:
print type(lookup).__name__, '-->', timeit.timeit(
'n in lookup; y in lookup', 'from __main__ import lookup, n, y')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment