Skip to content

Instantly share code, notes, and snippets.

@rvprasad
Last active December 24, 2019 22:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rvprasad/f9c97b1eff3c9b6df92ba3ac7815bd49 to your computer and use it in GitHub Desktop.
Save rvprasad/f9c97b1eff3c9b6df92ba3ac7815bd49 to your computer and use it in GitHub Desktop.
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()
f(data)
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.append(n)
uniq_elems = set(uniq_elems)
def extract_uniq_elems_using_set(nums):
uniq_elems = set()
for n in nums:
uniq_elems.add(n)
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