Created
July 9, 2022 17:51
-
-
Save jasonrahm/1a62a89bda69c548c430971395875abf to your computer and use it in GitHub Desktop.
evaluating intersection approaches
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import timeit | |
from statistics import mean | |
l1 = [1,2,3,4,5] | |
l2 = [3,4,5,6,7] | |
def intersect1(lst1, lst2): | |
res, lst2_copy = [], lst2[:] | |
for el in lst1: | |
if el in lst2_copy: | |
res.append(el) | |
lst2_copy.remove(el) | |
return res | |
def intersect2(lst1, lst2): | |
return [el for el in lst1 if el in lst2] | |
def a_test(): | |
intersect1(l1, l2) | |
def b_test(): | |
intersect2(l1, l2) | |
a_test_time = mean(timeit.repeat("a_test()", setup="from __main__ import a_test", repeat=10)) | |
b_test_time = mean(timeit.repeat("b_test()", setup="from __main__ import b_test", repeat=10)) | |
print(f'A test time: {a_test_time}, B test time: {b_test_time}') | |
print(f'B in reference to A: {100*(b_test_time - a_test_time) / a_test_time:.2f}%') | |
print(f'A in reference to B: {100*(a_test_time - b_test_time) / b_test_time:.2f}%') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment