forked from sovaa/submission-criteria
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbenchmark_base.py
36 lines (27 loc) · 1.12 KB
/
benchmark_base.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
30
31
32
33
34
35
36
import statistics
from datetime import datetime
class Benchmark(object):
def __init__(self, n_runs: int=5, print_checkpoint: bool=True):
self.n_runs = n_runs
self.print_checkpoint = print_checkpoint
def log(self, message: str) -> None:
print('[%s] - %s' % (datetime.now(), message))
def log_stats(self, times: list, unit: str='ms'):
self.log('[iteration %s/%s] %s' % (len(times), self.n_runs, self.format_stats(times, unit=unit)))
def format_stats(self, times: list, unit: str) -> str:
return 'median: %.2f%s, mean: %.2f%s, stdev: %.2f, max: %.2f%s, min: %.2f%s' % (
statistics.median(times), unit,
statistics.mean(times), unit,
statistics.stdev(times),
max(times), unit,
min(times), unit
)
def start(self, suffix: str=None):
if suffix is None:
suffix = '...'
else:
suffix = ': ' + suffix
self.log('starting benchmark%s' % suffix)
self.benchmark()
def benchmark(self):
raise NotImplementedError('method benchmark() not implemented yet')