-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbenchmark.py
More file actions
36 lines (31 loc) · 1.15 KB
/
benchmark.py
File metadata and controls
36 lines (31 loc) · 1.15 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
import timeit
import statistics
# try:
# from utils_db import insert_bench_results
# except ImportError:
# print(f'Could not import insert_bench_results from utils_db.py')
def benchmark(stmt, n, globals):
print('Function:', stmt)
times = timeit.repeat(stmt, number=1, globals=globals, repeat=n)
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: int | None = None) -> float:
timer = timeit.Timer(stmt, globals=globals)
print('Function:', stmt)
if n is None:
count, total_time = timer.autorange()
else:
count = n
total_time = timer.timeit(number=n)
print(' --------------')
print(f' Total time: {total_time}')
print(f' Count: {count}')
print(f' Mean: {(avg_time := total_time / count)}')
return avg_time