Created
December 2, 2014 21:00
-
-
Save yosemitebandit/5c5e4fe2817895a25f5f to your computer and use it in GitHub Desktop.
using timeit
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
# 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