Created
November 25, 2015 15:30
-
-
Save andersonvom/4d7e551b4c0418de3160 to your computer and use it in GitHub Desktop.
This is why you should use sets and not roll out your own implementation of set operations in python (or any language)
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 time | |
stop = 100000 | |
difference_count = 10000 | |
all_keys = range(stop) | |
random.shuffle(all_keys) | |
diff_keys = [random.randint(0, stop) for i in range(difference_count)] | |
s = time.time() | |
allowed_keys = [key for key in all_keys if key not in diff_keys] | |
e = time.time() | |
print(e - s) | |
# => ~15.11s | |
s = time.time() | |
allowed_keys = list(set(all_keys).difference(diff_keys)) | |
e = time.time() | |
print(e - s) | |
# => ~0.01s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment