Skip to content

Commit 654d01d

Browse files
authored
Update examples.py
1 parent 7388971 commit 654d01d

1 file changed

Lines changed: 18 additions & 0 deletions

File tree

optimize-python-code/examples.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,21 @@ def square_numbers_comprehension(numbers):
2525
print(f"Comprehension time: {comprehension_time:.4f} seconds")
2626
print(f"Improvement: {loop_time / comprehension_time:.2f}x faster")
2727

28+
# before
29+
def find_common_elements_list(list1, list2):
30+
common = []
31+
for item in list1: # Go through each item in the first list
32+
if item in list2: # Check if it exists in the second list
33+
common.append(item) # If yes, add it to our common list
34+
return common
35+
36+
# Test with reasonably large lists
37+
large_list1 = list(range(10000))
38+
large_list2 = list(range(5000, 15000))
39+
40+
start_time = time.time()
41+
common_list = find_common_elements_list(large_list1, large_list2)
42+
list_time = time.time() - start_time
43+
print(f"List approach time: {list_time:.4f} seconds")
44+
45+

0 commit comments

Comments
 (0)