-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtimer.py~
executable file
·140 lines (102 loc) · 4.16 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
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
from __future__ import print_function
import time
import numpy as np
class Timer:
times = []
ctimes = []
def __init__(self, names, components=[]):
self.names = names
self.n = len(names)
for i in range(self.n):
self.times.append( [] )
self.components = components
self.nc = len(self.components)
for i in range(self.nc):
self.ctimes.append( [] )
def _look_name(self, name):
for i, ref_name in enumerate(self.names):
if name == ref_name:
return i
return 99
def _look_comp(self, name):
for i, ref_name in enumerate(self.components):
if name == ref_name:
return i
return 99
def start(self, name):
indx = self._look_name(name)
self.times[indx] = [] #purge old list
self.times[indx].append( time.time() )
def purge(self, name):
indx = self._look_name(name)
self.times[indx] = [] #purge old list
#self.times[indx].append( time.time() )
def purge_comps(self):
for name in self.components:
indx = self._look_comp(name)
self.ctimes[indx] = []
def stop(self, name):
t0 = time.time() #stop before lookup
indx = self._look_name(name)
self.times[indx].append( t0 )
def lap(self, name):
t0 = time.time() #stop before lookup
indx = self._look_name(name)
self.times[indx].append( t0 )
def _calc_mean(self, arr):
x = np.zeros(len(arr)-1)
for ai in range(0, len(arr)-1):
x[ai] = arr[ai+1] - arr[ai]
return x
def start_comp(self, name):
t0 = time.time() #stop before lookup
indx = self._look_comp(name)
self.ctimes[indx].append( t0 )
def stop_comp(self, name):
t0 = time.time() #stop before lookup
indx = self._look_comp(name)
self.ctimes[indx][-1] = t0 - self.ctimes[indx][-1]
def stats(self, name):
indx = self._look_name(name)
ts = np.array( self.times[indx] )
cnts = len(ts) - 1
if cnts < 1:
return
t0 = ts[-1] - ts[0]
if t0 < 1.0e-6:
print("--- {:.10} time: {:8.5f} mus / {:3d} ({})".format(name.ljust(10), t0/1.0e6, cnts, t0))
elif 1.0e-6 < t0 < 1.0e-3:
print("--- {:.10} time: {:8.5f} ms / {:3d} ({})".format(name.ljust(10), t0/1.0e3, cnts, t0))
elif 1.0e-3 < t0:
print("--- {:.10} time: {:8.5f} s / {:3d} ({})".format(name.ljust(10), t0, cnts, t0))
#print additional statistics
if cnts > 1:
tss = self._calc_mean(ts)
tavg = np.mean(tss)
tstd = np.std(tss)
print("--- avg: {:8.5f} s / {:3d} ({})".format(tavg, cnts, tavg))
print("--- std: {:8.5f} s / {:3d} ({})".format(tstd, cnts, tstd))
def comp_stats(self):
print("--------------------------------------------------")
for name in self.components:
indx = self._look_comp(name)
ts = np.array( self.ctimes[indx] )
tavg = np.mean(ts)
cnts = len(ts)
tstd = np.std(ts)
t0 = np.sum(ts)
if t0 < 1.0e-6:
print("--- {:.10 } time: {:8.5f} mus / {:3d} ({})".format(name.ljust(10), t0/1.0e6, cnts, t0))
elif 1.0e-6 < t0 < 1.0e-3:
print("--- {:.10} time: {:8.5f} ms / {:3d} ({})".format(name.ljust(10), t0/1.0e3, cnts, t0))
elif 1.0e-3 < t0:
print("--- {:.10} time: {:8.5f} s / {:3d} ({})".format(name.ljust(10), t0, cnts, t0))
if cnts > 1:
print("- - - avg: {:8.5f} s / {:3d} ({})".format(tavg, cnts, tavg))
print("- - - std: {:8.5f} s / {:3d} ({})".format(tstd, cnts, tstd))
print("--------------------------------------------------")
def dump(self):
print("--------------------------------------------------")
for name in self.names:
self.stats(name)
print("--------------------------------------------------")