-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathbenchmarkrun.py
More file actions
75 lines (63 loc) · 2.33 KB
/
Copy pathbenchmarkrun.py
File metadata and controls
75 lines (63 loc) · 2.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
from time import perf_counter_ns
class BenchmarkRun:
def __init__(self, name, iterations, machine_readable):
self.name = name
self.iterations = iterations
self.timer_started = False
self.start = None
self.stop = None
self.operations = iterations
self.machine_readable = machine_readable
def start_timer(self):
self.timer_started = True
self.start = perf_counter_ns()
def stop_timer(self):
self.timer_started = False
self.stop = perf_counter_ns()
def get_runtime_ms(self):
val = self.get_runtime_ns()
return val / 1000000 if val != None else None
def get_runtime_s(self):
val = self.get_runtime_ns()
return val / 1000000000 if val != None else None
def get_runtime_ns(self):
if self.timer_started or self.stop == None:
return None
else:
return self.stop - self.start
def print_result(self):
if self.machine_readable:
print(f"{self.name},{self.get_runtime_s()},{self.operations}")
else:
name_col = self.name.ljust(60," ")
runtime_col = f"{self.get_runtime_s():.3f}".rjust(8," ") + " seconds"
operations_col = f"{int(self.operations/self.get_runtime_s())}/s".rjust(12," ")
print(f"{name_col} | {runtime_col} | {operations_col}")
def print_empty(self):
if self.machine_readable:
print(f"{self.name},-,-")
else:
name_col = self.name.ljust(60," ")
runtime_col = "-".rjust(8," ") + " seconds"
operations_col = "-/s".rjust(12," ")
print(f"{name_col} | {runtime_col} | {operations_col}")
def append_to_benchmark_list_and_run(_list, run, func):
_list.append(run)
try:
func(run)
run.print_result()
except:
run.print_empty()
def print_result_table_header():
name_col = "Name".ljust(60," ")
runtime_col = "Duration".ljust(16," ")
operations_col = f"Operations/s".rjust(12," ")
header = f"{name_col} | {runtime_col} | {operations_col}"
print(header+"\n"+len(header)*"-")
def print_machine_readable_header():
print("name,elapsed,operations")
def print_header(machine_readable):
if machine_readable:
print_machine_readable_header()
else:
print_result_table_header()