2
2
import matplotlib .pyplot as plt
3
3
import sol2 # Assuming sol2 is provided
4
4
import sol1 # Assuming sol1 is provided
5
- import sol3 # Since sol3 is nearly identical to sol2, we alias it for clarity
5
+ import sol3 # Assuming sol3 is provided and similar to sol2
6
+
7
+ def run_case (binary_str , n , solution , header ):
8
+ start_time = time .perf_counter_ns ()
9
+ output_sol = solution .extract_primes (binary_str , n )
10
+ elapsed_time_ns = time .perf_counter_ns () - start_time
11
+
12
+ if elapsed_time_ns < 1_000 : # Less than 1µs
13
+ formatted_time = f"{ elapsed_time_ns } ns"
14
+ elif elapsed_time_ns < 1_000_000 : # Less than 1ms
15
+ formatted_time = f"{ elapsed_time_ns / 1_000 :.3f} µs"
16
+ elif elapsed_time_ns < 1_000_000_000 : # Less than 1s
17
+ formatted_time = f"{ elapsed_time_ns / 1_000_000 :.3f} ms"
18
+ else :
19
+ formatted_time = f"{ elapsed_time_ns / 1_000_000_000 :.6f} s"
20
+
21
+ elapsed_time_s = elapsed_time_ns / 1_000_000_000 # Convert to seconds
22
+ return f" { header } : \n Time taken: { formatted_time } \n Answer: { output_sol } " , elapsed_time_s
23
+
24
+ # Define test cases
6
25
test_cases = [
7
- ("0100001101001111" , 999999 ), # 1
8
- ("01000011010011110100110101010000" , 999999 ), # 2
9
- ("1111111111111111111111111111111111111111" , 999999 ), # 3
10
- ("010000110100111101001101010100000011000100111000" , 999999999 ), # 4
11
- ("01000011010011110100110101010000001100010011100000110001" , 123456789012 ), # 5
12
- ("0100001101001111010011010101000000110001001110000011000100111001" , 123456789012345 ), # 6
13
- ("010000110100111101001101010100000011000100111000001100010011100100100001" , 123456789012345678 ), # 7
14
- ("01000011010011110100110101010000001100010011100000110001001110010010000101000001" , 1234567890123456789 ), # 8
15
- ("0100001101001111010011010101000000110001001110000011000100111001001000010100000101000100" , 1234567890123456789 ), # 9
16
- ("010000110100111101001101010100000011000100111000001100010011100100100001010000010100010001010011" , 12345678901234567890 ) # 10
26
+ ("0100001101001111" , 999999 ),
27
+ ("01000011010011110100110101010000" , 999999 ),
28
+ ("1111111111111111111111111111111111111111" , 999999 ),
29
+ ("010000110100111101001101010100000011000100111000" , 999999999 ),
30
+ ("01000011010011110100110101010000001100010011100000110001" , 123456789012 ),
31
+ ("0100001101001111010011010101000000110001001110000011000100111001" , 123456789012345 ),
32
+ ("010000110100111101001101010100000011000100111000001100010011100100100001" , 123456789012345678 ),
33
+ ("01000011010011110100110101010000001100010011100000110001001110010010000101000001" , 1234567890123456789 ),
34
+ ("0100001101001111010011010101000000110001001110000011000100111001001000010100000101000100" , 1234567890123456789 ),
35
+ ("010000110100111101001101010100000011000100111000001100010011100100100001010000010100010001010011" , 12345678901234567890 ),
17
36
]
18
37
19
- # Store execution times and results for each solution
38
+ # Store execution times
20
39
execution_times_sol1 = []
21
40
execution_times_sol2 = []
22
41
execution_times_sol3 = []
25
44
# Run performance tests
26
45
for idx , (binary_str , n ) in enumerate (test_cases ):
27
46
print (f"Test Case { idx + 1 } " )
47
+
48
+ result , time_sol1 = run_case (binary_str , n , sol1 , "Solution 1" )
49
+ execution_times_sol1 .append (time_sol1 )
50
+ print (result )
28
51
29
- # Solution 1
30
- start_time = time .time ()
31
- output_sol1 = sol1 .extract_primes (binary_str , n )
32
- elapsed_time_sol1 = time .time () - start_time
33
- execution_times_sol1 .append (elapsed_time_sol1 )
34
- print (f"Solution 1 - trail only:\n Time taken: { elapsed_time_sol1 :.6f} s\n Answer: { output_sol1 } " )
52
+ result , time_sol2 = run_case (binary_str , n , sol2 , "Solution 2" )
53
+ execution_times_sol2 .append (time_sol2 )
54
+ print (result )
35
55
36
- # Solution 2
37
- start_time = time .time ()
38
- output_sol2 = sol2 .extract_primes (binary_str , n )
39
- elapsed_time_sol2 = time .time () - start_time
40
- execution_times_sol2 .append (elapsed_time_sol2 )
41
- print (f"Solution 2 -sieve only:\n Time taken: { elapsed_time_sol2 :.6f} s\n Answer: { output_sol2 } " )
42
-
43
- # Solution2 - old version BAD
44
- start_time = time .time ()
45
- output_sol3 = sol3 .extract_primes (binary_str , n )
46
- elapsed_time_sol3 = time .time () - start_time
47
- execution_times_sol3 .append (elapsed_time_sol3 )
48
- print (f"Solution 2- the holy trinity:\n Time taken: { elapsed_time_sol3 :.6f} s\n Answer: { output_sol3 } \n " )
56
+ result , time_sol3 = run_case (binary_str , n , sol3 , "Solution 2 - holy trinity" )
57
+ execution_times_sol3 .append (time_sol3 )
58
+ print (result )
49
59
50
60
# Plot execution times
51
61
plt .figure (figsize = (10 , 5 ))
52
- plt .plot (test_case_indices , execution_times_sol1 , marker = 'o' , linestyle = '-' , label = "Solution 1- trial only" )
53
- plt .plot (test_case_indices , execution_times_sol2 , marker = 's' , linestyle = '--' , label = "Solution 2 - sieve only" )
54
- plt .plot (test_case_indices , execution_times_sol3 , marker = 'd' , linestyle = '-.' , label = "Solution 2- the holy trinity" )
62
+ plt .plot (test_case_indices , execution_times_sol1 , marker = 'o' , linestyle = '-' , label = "Solution 1" )
63
+ plt .plot (test_case_indices , execution_times_sol2 , marker = 's' , linestyle = '--' , label = "Solution 2" )
64
+ plt .plot (test_case_indices , execution_times_sol3 , marker = 'd' , linestyle = '-.' , label = "Solution 2 - the holy trinity" )
65
+
66
+ plt .xlabel ("Test Case Number" )
67
+ plt .ylabel ("Execution Time (s)" )
68
+ plt .title ("Execution Time Comparison: Solution 1, 2, and 3" )
69
+ plt .xticks (test_case_indices )
70
+ plt .grid (True )
71
+ plt .legend ()
72
+ plt .show ()
73
+
74
+ # Plot execution times for only Solution 2 - holy trinity
75
+ plt .figure (figsize = (10 , 5 ))
76
+ plt .plot (test_case_indices , execution_times_sol3 , marker = 'd' , linestyle = '-.' , color = 'r' , label = "Solution 2 - the holy trinity" )
55
77
56
- # Labels and title
57
78
plt .xlabel ("Test Case Number" )
58
79
plt .ylabel ("Execution Time (s)" )
59
- plt .title ("Performance of extract_primes Function Across Test Cases " )
80
+ plt .title ("Execution Time: Solution 2 - The Holy Trinity " )
60
81
plt .xticks (test_case_indices )
61
82
plt .grid (True )
62
83
plt .legend ()
84
+ plt .show ()
63
85
64
- # Show the plot
65
- plt .show ()
0 commit comments