Skip to content

Instantly share code, notes, and snippets.

@andersonvom
Created November 25, 2015 15:30
Show Gist options
  • Save andersonvom/4d7e551b4c0418de3160 to your computer and use it in GitHub Desktop.
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)
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