-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtime_logger.py
65 lines (45 loc) · 1.85 KB
/
time_logger.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
from collections import defaultdict
import time
class TimeLogger:
def __init__(self):
self._section_to_time = defaultdict(float)
self._latest_start_time = defaultdict(float)
self._recursive_counter = defaultdict(int)
def start_section(self, name: str):
self._recursive_counter[name] += 1
if name not in self._latest_start_time:
self._latest_start_time[name] = time.perf_counter()
def end_section(self, name: str):
if name not in self._recursive_counter:
raise ValueError('This section was not started!')
self._recursive_counter[name] -= 1
if self._recursive_counter[name] == 0:
self._section_to_time[name] += time.perf_counter() - self._latest_start_time[name]
del self._recursive_counter[name]
del self._latest_start_time[name]
def get_measurements(self):
return self._section_to_time
def zero_all_measurements(self):
if len(self._recursive_counter) > 0:
raise RuntimeError('There are exist not ended sections!')
self._section_to_time = defaultdict(float)
self._latest_start_time = defaultdict(float)
self._recursive_counter = defaultdict(int)
TIME_LOGGER = TimeLogger()
def get_time_logger_results():
return TIME_LOGGER.get_measurements()
def log_time(func):
def wrapper(*args, **kwargs):
TIME_LOGGER.start_section(func.__name__)
res = func(*args, **kwargs)
TIME_LOGGER.end_section(func.__name__)
return res
return wrapper
class TimeLog():
def __init__(self, section_name):
self.section_name = section_name
def __enter__(self):
TIME_LOGGER.start_section(self.section_name)
return self
def __exit__(self, exc_type, exc_value, exc_traceback):
TIME_LOGGER.end_section(self.section_name)