Skip to content

Instantly share code, notes, and snippets.

@Veedrac
Last active August 29, 2015 14:22
Show Gist options
  • Save Veedrac/c343f3c2b0a2a563658a to your computer and use it in GitHub Desktop.
Save Veedrac/c343f3c2b0a2a563658a to your computer and use it in GitHub Desktop.
>>> python2 -m timeit -s 'r = {1, 2, 3}; s = {4, 5, 6}; t = {7, 8, 9}' '3 in reduce(lambda x,y :x.union(y),[r,s,t])'
1000000 loops, best of 3: 1.01 usec per loop
>>> python2 -m timeit -s 'r = {1, 2, 3}; s = {4, 5, 6}; t = {7, 8, 9}' '3 in r|s|t'
1000000 loops, best of 3: 0.399 usec per loop
>>> python2 -m timeit -s 'r = {1, 2, 3}; s = {4, 5, 6}; t = {7, 8, 9}' '3 in set().union(r, s, t)'
1000000 loops, best of 3: 0.407 usec per loop
>>> python2 -m timeit -s 'r = {1, 2, 3}; s = {4, 5, 6}; t = {7, 8, 9}' 'any(3 in item for item in [r,s,t])'
1000000 loops, best of 3: 0.621 usec per loop
>>> python2 -m timeit -s 'r = {1, 2, 3}; s = {4, 5, 6}; t = {7, 8, 9}' '3 in r.union(s).union(t)'
1000000 loops, best of 3: 0.528 usec per loop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment