Skip to content

Instantly share code, notes, and snippets.

@harabchuk
Created September 19, 2023 13:12
Show Gist options
  • Save harabchuk/fe32f0096cef0d7c16f4cad9fd5d5f85 to your computer and use it in GitHub Desktop.
Save harabchuk/fe32f0096cef0d7c16f4cad9fd5d5f85 to your computer and use it in GitHub Desktop.
Compare search in array vs set
import random
import string
import timeit
def generate_random_string(length):
return ''.join(random.choice(string.ascii_letters) for _ in range(length))
random_strings = [
generate_random_string(random.randint(7, 12)) for _ in range(500)
]
def find_in_array():
count = 0 # do some job so that optimiser does not skip searching in array
for _ in range(10000):
string_to_find = random.choice(random_strings) # existing string
count += 1 if string_to_find in random_strings else 2
return count
def find_in_set():
count = 0 # do some job so that optimiser does not skip searching in set
random_strings_set = set(
random_strings
) # include the set generation into total time
for _ in range(10000):
string_to_find = random.choice(random_strings) # existig string
count += 1 if string_to_find in random_strings_set else 2
return count
time_taken_array = timeit.timeit(find_in_array, number=1)
print(f"find string in array (10,000 runs): {time_taken_array:.10f} seconds")
time_taken_set = timeit.timeit(find_in_set, number=1)
print(f"find string in set (10,000 runs): {time_taken_set:.10f} seconds")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment