Skip to content

Instantly share code, notes, and snippets.

@9thbit
Created September 15, 2016 08:32
Show Gist options
  • Save 9thbit/1305c72bf95001704711bdaf688c5511 to your computer and use it in GitHub Desktop.
Save 9thbit/1305c72bf95001704711bdaf688c5511 to your computer and use it in GitHub Desktop.
Times checks for UUIDs in a large list versus a large set
from uuid import uuid4
import timeit
def build_uuid_list(num_uuids):
return [uuid4() for i in xrange(num_uuids)]
def main():
setup = """
from uuid import uuid4
from __main__ import build_uuid_list
import bisect
N = 30000
uuids_list = build_uuid_list(N)
uuids_set = set(uuids_list)
first_uuid = uuids_list[0]
mid_uuid = uuids_list[len(uuids_list) / 2]
last_uuid = uuids_list[-1]
non_exist_uuid = uuid4()
"""
test_codes = [
# In List
('first in list', 'first_uuid in uuids_list'),
('mid in list', 'mid_uuid in uuids_list'),
('last in list', 'last_uuid in uuids_list'),
('non_exist in list', 'non_exist_uuid in uuids_list'),
# Not in List
('first not in list', 'first_uuid not in uuids_list'),
('mid not in list', 'mid_uuid not in uuids_list'),
('last not in list', 'last_uuid not in uuids_list'),
('non_exist not in list', 'non_exist_uuid not in uuids_list'),
# In Set
('first in set', 'first_uuid in uuids_set'),
('mid in set', 'mid_uuid in uuids_set'),
('last in set', 'last_uuid in uuids_set'),
('non_exist in set', 'non_exist_uuid in uuids_set'),
# Not in Set
('first not in set', 'first_uuid not in uuids_set'),
('mid not in set', 'mid_uuid not in uuids_set'),
('last not in set', 'last_uuid not in uuids_set'),
('non_exist not in set', 'non_exist_uuid not in uuids_set'),
]
for test_name, test_code in test_codes:
times = timeit.repeat(test_code, setup, repeat=3, number=1000)
times_str = " ".join(["%7.3f" % t for t in times])
print "%-20s %s" % (test_name, times_str)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment