Created
September 15, 2011 22:40
-
-
Save cpdean/1220686 to your computer and use it in GitHub Desktop.
testing the 'in' operator #science
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
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