-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathStatProfiler.py
91 lines (76 loc) · 2.3 KB
/
StatProfiler.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import time
from math import sqrt
class StatProfiler():
""" Profiler class with statistics"""
def __init__(self, name):
self.N, self.agg, self.aggvar = 0, 0.0, 0.0
self.name=name
self.t0 = None
def __del__(self):
try:
mean = self.agg/self.N
stddev = sqrt((self.aggvar - self.N*mean**2)/(self.N-1))
except ZeroDivisionError:
mean = float('NaN')
stddev=float('NaN')
print(f"StatProfiler {self.name}: {self.N} reps, avg: {mean*1e3} ms, stddev: {stddev*1e3} ms, total: {self.agg} s")
def tic(self):
""" Matlab style """
self.t0=time.time()
def toc(self):
""" Matlab style """
if self.t0 is not None:
t = time.time()-self.t0
self.N+=1
self.agg+=t
self.aggvar+=t**2
def profile(self, func):
""" lambda style """
self.tic()
x = func()
self.toc()
return x
def decorate(self, func):
""" Decorator style"""
def ret(*args, **kwargs):
self.tic()
x = func(*args, **kwargs)
self.toc()
return x
return ret
class SSProfile(StatProfiler):
""" Singleton Stat Profilers """
_instances = dict()
def __new__(cls, name, **kwargs):
if name not in cls._instances:
print("instantiating singleton StatProfiler %s"%name)
cls._instances[name] = StatProfiler(name)
return cls._instances[name]
def test_no_runs():
SSProfile("test_none")
def test_all_tocs():
SSProfile("test_all_tocs").toc()
SSProfile("test_all_tocs").toc()
SSProfile("test_all_tocs").toc()
SSProfile("test_all_tocs").toc()
SSProfile("test_all_tocs").toc()
def test_decorator():
@SSProfile("decorator").decorate
def my_decorated_function():
time.sleep(0.0001)
for i in range(1000):
my_decorated_function()
def test_lambda():
for i in range(1000):
SSProfile("lambda").profile(lambda : time.sleep(0.0001))
def test_tic_toc():
for i in range(1000):
SSProfile("tic_toc").tic()
time.sleep(0.0001)
SSProfile("tic_toc").toc()
if __name__ == '__main__':
test_no_runs()
test_all_tocs()
test_decorator()
test_lambda()
test_tic_toc()