-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbenchmark.py
30 lines (27 loc) · 1013 Bytes
/
benchmark.py
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
import timeit
import statistics
def benchmark(stmt, n, globals):
times = timeit.repeat(stmt, number=1, globals=globals, repeat=n)
print('Function:', stmt)
print(' --------------')
print(f' Min: {min(times)}')
print(f' Median: {statistics.median(times)}')
print(f' Mean: {statistics.mean(times)}')
print(f' Stdev: {statistics.stdev(times) if len(times) > 1 else "N.A."}')
print(f' Max: {max(times)}')
print(' --------------')
print(f' samples: {len(times)}')
print()
def benchmark_autorange(stmt: str, globals: dict, n: None) -> float:
timer = timeit.Timer(stmt, globals=globals)
if n is None:
count, total_time = timer.autorange()
else:
count = n
total_time = timer.timeit(number=n)
print('Function:', stmt)
print(' --------------')
print(f' Total time: {total_time}')
print(f' Count: {count}')
print(f' Mean: {(avg_time := total_time / count)}')
return avg_time