-
Notifications
You must be signed in to change notification settings - Fork 5
/
utils_timer.py
69 lines (53 loc) · 1.66 KB
/
utils_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
"""
Timing utilities, by Robert-Jan Bruintjes; modified by yclin.
"""
import time
import numpy as np
import datetime
def start_timing():
start_time = time.time()
return start_time
def end_timing(timing_data):
"""Returns time in seconds."""
start_time = timing_data
end_time = time.time()
elapsed_time = end_time - start_time
return elapsed_time
class MyTimer(object):
def __init__(self, name=None, keep_n=10):
self.name = name
self.times = []
self.anomaly = False
self.keep = np.zeros((keep_n), dtype="float32")
self.keep_n = keep_n
self.keep_idx = 0
def write(self, time):
self.keep[self.keep_idx] = time
self.keep_idx = (self.keep_idx + 1) % self.keep_n
def __enter__(self):
self.timing = start_timing()
def __exit__(self, exc_type, exc_value, exc_traceback):
if not self.anomaly:
time = end_timing(self.timing)
self.write(time)
else:
self.anomaly = False
def start(self):
self.__enter__()
def stop(self):
self.__exit__(None, None, None)
def manual_entry(self, start_time, end_time):
if not self.anomaly:
elapsed_time = end_time - start_time
self.write(elapsed_time)
else:
self.anomaly = False
def mark_anomaly(self):
self.anomaly = True
def last(self):
return self.keep[(self.keep_idx - 1) % self.keep_n]
def mean(self):
return np.mean(self.keep)
def __str__(self):
name_display = f"{self.name}" if self.name is not None else "Time"
return f"{name_display} (s): {self.mean():.4f}"