Skip to content

Instantly share code, notes, and snippets.

@cpdean
Created September 15, 2011 22:40
Show Gist options
  • Save cpdean/1220686 to your computer and use it in GitHub Desktop.
Save cpdean/1220686 to your computer and use it in GitHub Desktop.
testing the 'in' operator #science
from time import time
def test(iterable):
start = time()
for i in range(scale):
if i in iterable:
a = 90.0 + 6000
return time()-start
def aggregate(count,f,a):
tests = [f(a) for c in range(count)]
return sum(tests)/count
scale = 2**13
c = 10
l = list(n for n in range(scale) if n%3==0)
d = dict((n,True) for n in range(scale) if n%3==0)
s = set(n for n in range(scale) if n%3==0)
print "done generating data, now processing"
print aggregate(c,test,l),"lists" # 1.9323
print aggregate(c,test,d),"dicts" # 0.0041 ## winner
print aggregate(c,test,s),"sets" # 0.0044 ## very close, maybe it's a wrapper for dict()?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment