File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -25,3 +25,21 @@ def square_numbers_comprehension(numbers):
2525print (f"Comprehension time: { comprehension_time :.4f} seconds" )
2626print (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+
You can’t perform that action at this time.
0 commit comments