Created
April 20, 2016 22:34
-
-
Save noncombatant/935f6e4c206a6d19ae0e2eafaa662779 to your computer and use it in GitHub Desktop.
Microbenchmark for different collection types in Python. What do you expect to happen?
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
def set_up_test(count): | |
l = [] | |
d = {} | |
s = set() | |
for i in range(count): | |
l.append(i) | |
d[i] = True | |
s.add(i) | |
return l, d, s | |
def stopwatch(container, count): | |
import time | |
start = time.time() | |
n = 0 | |
for i in range(count): | |
if i in container: | |
n += 1 | |
return time.time() - start, n | |
import sys | |
count = 10**5 | |
if len(sys.argv) > 1: | |
count = int(sys.argv[1]) | |
l, d, s = set_up_test(count) | |
print "dictionary", stopwatch(d, count) | |
print "set", stopwatch(s, count) | |
print "list", stopwatch(l, count) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment