Created
February 28, 2012 18:52
-
-
Save smarnach/1934353 to your computer and use it in GitHub Desktop.
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
import random | |
import timeit | |
def inter_reduce(sets): | |
return reduce(set.intersection, sets) | |
def inter_for(sets): | |
sets = iter(sets) | |
result = next(sets) | |
for s in sets: | |
result.intersection_update(s) | |
return result | |
random.seed(42) | |
sets = [set(random.randrange(1000) for i in range(4000)) | |
for j in range(100)] | |
print timeit.timeit("inter_reduce(sets)", | |
"from __main__ import inter_reduce, sets", number=1000) | |
print timeit.timeit("inter_for(sets)", | |
"from __main__ import inter_for, sets", number=1000) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
^^ as an addendum to this, my fork has versions that are referentially transparent, but this definitely adds significant overhead (still cheaper than reduce though). I'm pretty sure there's a better way of doing it as well.