Skip to content

Instantly share code, notes, and snippets.

@RhettTrickett
Created August 10, 2019 17:24
Show Gist options
  • Save RhettTrickett/88b674cb76f9e87fab7b2b987ce1739e to your computer and use it in GitHub Desktop.
Save RhettTrickett/88b674cb76f9e87fab7b2b987ce1739e to your computer and use it in GitHub Desktop.
Searching lists vs. sets
import random
import string
from time import time
def random_string(length=20):
chars = string.ascii_letters
return ''.join(random.choice(chars) for i in range(length))
# Prepare haystack and needles
haystack = [random_string() for i in range(99000)]
needles = [random_string() for i in range(1000)]
haystack.extend(needles)
random.shuffle(haystack)
#Time haystack look-ups as a list
list_start = time()
for n in needles:
if n in haystack:
pass
list_duration = time() - list_start
print(f"Found needles in list in {format(list_duration, 'f')} seconds.")
# Found needles in list in 4.129141 seconds.
# Create set and add the same haystack with needles
set_haystack = set()
set_haystack.update(haystack)
# Time set look-ups for needles
set_start = time()
for n in needles:
if n in set_haystack:
pass
set_duration = time() - set_start
print(f"Found needles in set in {format(set_duration, 'f')} seconds.")
# Found needles in set in 0.000150 seconds.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment