Skip to content

Instantly share code, notes, and snippets.

@noncombatant
Created April 20, 2016 22:34
Show Gist options
  • Save noncombatant/935f6e4c206a6d19ae0e2eafaa662779 to your computer and use it in GitHub Desktop.
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?
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