-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathTimer.py
More file actions
55 lines (48 loc) · 1.99 KB
/
Timer.py
File metadata and controls
55 lines (48 loc) · 1.99 KB
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
import time
import logging
logger = logging.getLogger('Timer')
class Timer:
def __init__(self):
self.counters = {}
def tic(self, name, n=20, print_freq=0):
if not (name in self.counters):
self.counters[name] = {
'tic': time.time(),
'n': n,
'k': 0,
'print_freq': print_freq,
'lastN': [0 for i in range(0, n)],
'lastN_val': [0 for i in range(0, n)],
'rolling_val_sum': 0
}
else:
self.counters[name]['tic'] = time.time()
def toc(self, name, val = None):
toc = time.time()
counter = self.counters[name]
k = counter['k']
n = counter['n']
k_mod_n = k % n
counter['lastN'][ k_mod_n ] = toc - counter['tic']
if not (val is None):
counter['lastN_val'][ k_mod_n ] = val
self.maybe_emit(name, not (val is None))
counter['k'] = k + 1
def maybe_emit(self, name, show_val_per_second):
counter = self.counters[name]
k = counter['k']
n = counter['n']
if show_val_per_second:
lastN = counter['lastN']
lastN_val = counter['lastN_val']
sum_s = 0.0 + sum([lastN[i % n ] for i in range(max(k - n,0), k)])
sum_val = 0.0 + sum([lastN_val[i % n ] for i in range(max(k - n,0), k)])
counter['rolling_val_sum'] = counter['rolling_val_sum'] + sum_val
if counter['print_freq'] > 0 and (k % counter['print_freq'] == 0):
if sum_s > 0:
logger.info('%s : %s / s (%s total)' % (name, sum_val / sum_s, counter['rolling_val_sum']))
else:
if counter['print_freq'] > 0 and (k % counter['print_freq'] == 0):
lastN = counter['lastN']
sum_s = 0.0 + sum([lastN[i % n ] for i in range(max(k - n,0), k)])
logger.info('%s : %ss' % (name, sum_s / min(n, k + 1)))