Skip to content

Instantly share code, notes, and snippets.

@djipko
Last active August 29, 2015 14:17
Show Gist options
  • Select an option

  • Save djipko/6564b8e41a24da05fbb9 to your computer and use it in GitHub Desktop.

Select an option

Save djipko/6564b8e41a24da05fbb9 to your computer and use it in GitHub Desktop.
import random
import time
import uuid
if __name__ == "__main__":
# 1k instance per host on the heavy side
uuid_list = [str(uuid.uuid4()) for x in xrange(1000)]
# 100 claims also on the heavy side
uuids_to_check = [str(uuid.uuid4()) for x in xrange(100)]
# Let's say we will find 10 on average
for _ in xrange(random.randint(0, 9)):
uuid_list[random.randint(0, 999)] = uuids_to_check[random.randint(0, 99)]
uuid_set = set(uuid_list)
def checker(list_or_set):
found = 0
for u in uuids_to_check:
if u in list_or_set:
found += 1
return found
start = time.time()
found = checker(uuid_list)
time_list = time.time() - start
start = time.time()
checker(uuid_set)
time_set = time.time() - start
print("found %s, list time: %s, set time: %s" %
(found, time_list, time_set))
# on my laptop
# [ndipanov@ndipanov-thinkpad ~]$ python uuid_set_vs_list.py
# found 1, list time: 0.00192785263062, set time: 1.81198120117e-05
# [ndipanov@ndipanov-thinkpad ~]$ python uuid_set_vs_list.py
# found 8, list time: 0.00124907493591, set time: 1.81198120117e-05
# [ndipanov@ndipanov-thinkpad ~]$ python uuid_set_vs_list.py
# found 4, list time: 0.00167679786682, set time: 1.59740447998e-05
# [ndipanov@ndipanov-thinkpad ~]$ python uuid_set_vs_list.py
# found 1, list time: 0.00125813484192, set time: 1.19209289551e-05
# [ndipanov@ndipanov-thinkpad ~]$ python uuid_set_vs_list.py
# found 3, list time: 0.00122785568237, set time: 1.31130218506e-05
# [ndipanov@ndipanov-thinkpad ~]$ python uuid_set_vs_list.py
# found 2, list time: 0.00131583213806, set time: 1.28746032715e-05
# [ndipanov@ndipanov-thinkpad ~]$ python uuid_set_vs_list.py
# found 7, list time: 0.00173497200012, set time: 1.69277191162e-05
# [ndipanov@ndipanov-thinkpad ~]$ python uuid_set_vs_list.py
# found 4, list time: 0.00144982337952, set time: 1.50203704834e-05
# [ndipanov@ndipanov-thinkpad ~]$ python uuid_set_vs_list.py
# found 6, list time: 0.00186896324158, set time: 1.90734863281e-05
# [ndipanov@ndipanov-thinkpad ~]$ python uuid_set_vs_list.py
# found 6, list time: 0.00121998786926, set time: 1.19209289551e-05
# [ndipanov@ndipanov-thinkpad ~]$ python uuid_set_vs_list.py
# found 0, list time: 0.00156593322754, set time: 1.97887420654e-05
# [ndipanov@ndipanov-thinkpad ~]$ python uuid_set_vs_list.py
# found 7, list time: 0.00124287605286, set time: 1.31130218506e-05
# [ndipanov@ndipanov-thinkpad ~]$ python uuid_set_vs_list.py
# found 4, list time: 0.0017671585083, set time: 1.69277191162e-05
# [ndipanov@ndipanov-thinkpad ~]$ python uuid_set_vs_list.py
# found 9, list time: 0.00120806694031, set time: 1.19209289551e-05
#
# Several orders of magnitude faster as is to be expected
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment