22
22
23
23
from pathlib import Path
24
24
import time
25
- from typing import List
25
+ from typing import Callable , List , Tuple
26
26
import random
27
27
import subprocess
28
28
import argparse
29
29
30
30
ROOT_PATH = Path (__file__ ).parent .parent
31
31
BUILD_PATH_DEFAULT = "build"
32
32
33
+ FUNCTIONS = {
34
+ "abs" : lambda x , y : abs (x ),
35
+ "add" : lambda x , y : x + y ,
36
+ "comparison" : lambda x , y : x > y ,
37
+ "division" : lambda x , y : x / y ,
38
+ "multiply" : lambda x , y : x * y ,
39
+ "subtract" : lambda x , y : x - y ,
40
+ "root" : lambda x , y : x ** (1 / y ),
41
+ "power" : lambda x , y : x ** y ,
42
+ }
43
+
44
+
45
+ def _benchmark_function (
46
+ function : Callable ,
47
+ input_1 : int ,
48
+ input_2 : int ,
49
+ ) -> float :
50
+ start = time .time ()
51
+ try :
52
+ function (input_1 , input_2 )
53
+ except Exception as e :
54
+ print (f"Error: { e } " )
55
+ end = time .time ()
56
+ return end - start
57
+
33
58
34
59
def _benchmark (
35
60
cmd : str ,
@@ -60,21 +85,32 @@ def _benchmark(
60
85
except subprocess .TimeoutExpired :
61
86
print (f"Warning: Timeout running { cmd } on { input_str1 } , { input_str2 } " )
62
87
end = time .time ()
63
- print (f"Time taken: { end - start } \033 [A\r " )
64
88
return end - start
65
89
66
90
67
91
def benchmark (
68
92
cmd : str , inputs : List [str ], limit : int , verbose : bool = False , timeout : int = 10
69
- ) -> List [float ]:
70
- random_number = str (random .randint (0 , limit ))
93
+ ) -> Tuple [List [float ], List [float ], bool ]:
94
+ random_number = random .randint (0 , limit )
95
+ random_number_str = str (random_number )
71
96
time_needed = []
97
+ time_needed_system = []
98
+ system_available = False
72
99
for input_str in inputs :
73
100
time_needed .append (
74
- _benchmark (cmd , input_str , random_number , verbose = verbose , timeout = timeout )
101
+ _benchmark (
102
+ cmd , input_str , random_number_str , verbose = verbose , timeout = timeout
103
+ )
75
104
)
105
+ if (func := Path (cmd ).stem ) in FUNCTIONS :
106
+ time_needed_system .append (
107
+ _benchmark_function (FUNCTIONS [func ], int (input_str ), random_number )
108
+ )
109
+ system_available = True
110
+ else :
111
+ time_needed_system .append (0 )
76
112
77
- return time_needed
113
+ return time_needed , time_needed_system , system_available
78
114
79
115
80
116
def main ():
@@ -90,7 +126,7 @@ def main():
90
126
"-e" , "--executables" , type = str , default = "bin/*" , required = False
91
127
)
92
128
parser .add_argument ("-t" , "--timeout" , type = int , default = 50 , required = False )
93
- parser .add_argument ("-l" , "--limit" , type = float , default = 500 , required = False )
129
+ parser .add_argument ("-l" , "--limit" , type = float , default = 100 , required = False )
94
130
parser .add_argument ("-v" , "--verbose" , action = "store_true" , required = False )
95
131
parser .add_argument ("-g" , "--graph" , action = "store_true" , required = False )
96
132
args = parser .parse_args ()
@@ -125,9 +161,11 @@ def main():
125
161
print (f"Running benchmarks on { exe } " )
126
162
inputs_system = [i for i in range (LIMIT )]
127
163
inputs = [str (i ) for i in inputs_system ]
128
- times = benchmark (
164
+ time_result = benchmark (
129
165
str (exe ), inputs , LIMIT , verbose = args .verbose , timeout = args .timeout
130
166
)
167
+ times = time_result [0 ]
168
+ times_system = time_result [1 ]
131
169
if (not args .graph ) and args .verbose :
132
170
print (f"Times: { times } " )
133
171
if args .graph :
@@ -137,7 +175,11 @@ def main():
137
175
import matplotlib .pyplot as plt
138
176
139
177
plt .figure (figsize = (15 , 5 ))
140
- plt .plot (range (LIMIT ), times )
178
+ plt .plot (range (LIMIT ), times , label = "Steppable" )
179
+ if time_result [2 ]:
180
+ plt .plot (range (LIMIT ), times_system , label = "System" )
181
+
182
+ plt .legend ()
141
183
plt .xlabel ("Input" )
142
184
plt .ylabel ("Time (s)" )
143
185
plt .title (f"Benchmark for { exe .stem } " )
0 commit comments