-
Notifications
You must be signed in to change notification settings - Fork 9
/
timer.py
executable file
·49 lines (41 loc) · 1.26 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
import time
class Timer:
def __init__(self):
self.st = {}
self.delta = {}
def clear(self, name=None):
if name is None:
self.st = {}
self.delta = {}
elif isinstance(name, list) or isinstance(name, tuple):
for x in name:
self.st.pop(x, None)
self.delta.pop(x, None)
else:
self.st.pop(name, None)
self.delta.pop(name, None)
def tic(self, name='default'):
self.st[name] = time.perf_counter()
def toc(self, name='default'):
if name not in self.st:
return -1
t = time.perf_counter()
dt = t - self.st[name]
self.st[name] = t
if name not in self.delta:
self.delta[name] = [dt]
else:
self.delta[name].append(dt)
return dt
def avg(self, name='default'):
if name not in self.delta:
return -1
return sum(self.delta[name]) / len(self.delta[name])
def tot(self, name='default'):
if name not in self.delta:
return -1
return sum(self.delta[name])
def last(self, name='default'):
if name not in self.delta:
return -1
return self.delta[name][-1]