Skip to content

Instantly share code, notes, and snippets.

@nod
Created September 27, 2017 16:07
Show Gist options
  • Save nod/9b27f3e335726dee82f81d8bd959c2be to your computer and use it in GitHub Desktop.
Save nod/9b27f3e335726dee82f81d8bd959c2be to your computer and use it in GitHub Desktop.
import random
import time
s = set()
for x in xrange(1000):
s.add(str(random.randint(1,1000000)))
d = {x:1 for x in s}
n = d.keys()[:400]
n.extend([ str(random.randint(1,1000000)) for i in xrange(1000) ])
def test(col):
res = 0
start = time.time()
for x in xrange(1000):
for i in n:
res += 1 if i in col else 0
end = time.time()
print type(col),"elapsed:", end - start
test(s)
test(d)
test(s)
test(d)
test(s)
test(d)
test(s)
test(d)
======= run results ========
mac:~$ python x.py
<type 'set'> elapsed: 0.0853579044342
<type 'dict'> elapsed: 0.0813100337982
<type 'set'> elapsed: 0.0829548835754
<type 'dict'> elapsed: 0.0841579437256
<type 'set'> elapsed: 0.0930590629578
<type 'dict'> elapsed: 0.0867938995361
<type 'set'> elapsed: 0.0977139472961
<type 'dict'> elapsed: 0.0818090438843
mac:~$ python x.py
<type 'set'> elapsed: 0.0851221084595
<type 'dict'> elapsed: 0.0833868980408
<type 'set'> elapsed: 0.0867528915405
<type 'dict'> elapsed: 0.0816919803619
<type 'set'> elapsed: 0.0821380615234
<type 'dict'> elapsed: 0.0829949378967
<type 'set'> elapsed: 0.083251953125
<type 'dict'> elapsed: 0.0812418460846
@nod
Copy link
Author

nod commented Sep 27, 2017

btw, this was run on python 2.7.10 on macbook pro

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment