-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimer.py
66 lines (51 loc) · 1.8 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
import time
from typing import Dict
from tabulate import tabulate
class Timer:
def __init__(self, name=None):
self.name = name
self.wipe()
def wipe(self):
self.task_times = {}
self.task_iterations = {}
self.current_task = None
self.current_start_time = None
def task(self, name):
if self.current_task:
self.__clear_certain()
self.current_task = name
self.current_start_time = time.time()
def clear(self):
if self.current_task:
self.__clear_certain()
def __clear_certain(self):
self.task_times[self.current_task] = self.task_times.get(self.current_task, 0) + \
(time.time() - self.current_start_time)
self.task_iterations[self.current_task] = self.task_iterations.get(self.current_task, 0) + 1
self.current_task = None
def log(self, logger=None):
total_time_logged = sum(self.task_times.values())
s = f"Timer log: {self.name}\n"
s += tabulate(
[
(
task_name,
self.task_times[task_name],
f"{round(100 * self.task_times[task_name] / total_time_logged, 1)}%",
self.task_iterations[task_name],
self.task_times[task_name]/self.task_iterations[task_name]
)
for task_name in self.task_times
],
("Task name", "Total time", "Percent", "Number of iterations", "Average time"),
)
if logger:
logger.info(s)
else:
print(s)
@staticmethod
def get(name):
if name not in TIMERS:
TIMERS[name] = Timer(name)
return TIMERS[name]
TIMERS: Dict[str, Timer] = {}