Skip to content

Instantly share code, notes, and snippets.

Last active December 24, 2019 22:51
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Compare performance of set and list in identifying unique values
import random
import statistics
from timeit import default_timer as timer
def time_function(f, data):
times = []
for _ in range(0, 20):
start = timer()
times.append(timer() - start)
print(f'{f.__name__} Mean Time: {statistics.mean(times)} Median Time:{statistics.median(times)}')
def extract_uniq_elems_using_list(nums):
uniq_elems = []
for n in nums:
uniq_elems = set(uniq_elems)
def extract_uniq_elems_using_set(nums):
uniq_elems = set()
for n in nums:
not_so_randoms = [random.randint(0, 10000) for i in range(0, 10000)]
randoms = [random.randint(0, 10000) for i in range(0, 10000)]
for i in range(1, 14):
print(f'Nums: {len(randoms)}')
time_function(extract_uniq_elems_using_set, not_so_randoms)
time_function(extract_uniq_elems_using_list, not_so_randoms)
time_function(extract_uniq_elems_using_set, randoms)
time_function(extract_uniq_elems_using_list, randoms)
not_so_randoms *= 2
randoms.extend(random.randint(0, len(randoms)) for i in range(0, len(randoms)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment