-
Notifications
You must be signed in to change notification settings - Fork 17
/
timer.py
63 lines (46 loc) · 1.8 KB
/
timer.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
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
#
# Timer
# Written by Qhan
#
from __future__ import print_function
import time
import numpy as np
class Timer():
def __init__(self, update=True):
self.stage, self.start = {}, {}
self.update = update
self.start_anonymous = time.time() * 1000
def tic(self, name=None):
if name:
self.start[name] = time.time() * 1000
else:
self.start_anonymous = time.time() * 1000
def toc(self, name=None):
if name in self.start.keys():
period = time.time() * 1000 - self.start[name]
if name not in self.stage.keys(): self.add_stage(name)
self.update_min_max(name, period)
else:
period = time.time() * 1000 - self.start_anonymous
return period
def add_stage(self, name):
if name not in self.stage.keys():
self.stage[name] = {'min': np.inf, 'max': 0, 'avg': 0}
def del_stage(self, name):
self.stage.pop(name, None)
def enable_update(self):
self.update = True
def disable_update(self):
self.update = False
def update_min_max(self, name, t):
new_avg = 0.0
if self.update and name in self.stage.keys():
if t < self.stage[name]['min']: self.stage[name]['min'] = t
if t > self.stage[name]['max']: self.stage[name]['max'] = t
new_avg = self.stage[name]['avg'] * 0.9 + t * 0.1
self.stage[name]['avg'] = new_avg
#print(name + ': %7.4f ms, ' % (new_avg), end='')
def summary(self):
print('\n%15s: %8s %12s %12s' % ('Stage', 'Min', 'Max', 'Avg'))
for name, t in self.stage.items():
print('%15s' % (name) + ': %8.4f ms, %8.4f ms, %8.4f ms' % (t['min'], t['max'], t['avg']))