-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathtimer.py
More file actions
31 lines (23 loc) · 788 Bytes
/
timer.py
File metadata and controls
31 lines (23 loc) · 788 Bytes
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
import time
from collections import defaultdict
from contextlib import contextmanager
from typing_extensions import Self
class Timer:
_times: defaultdict
def __init__(self):
self._times = defaultdict(float)
@contextmanager
def measure(self, name: str):
start = time.monotonic()
try:
yield
finally:
end = time.monotonic()
self._add_time(name, end - start)
def _add_time(self, name: str, val: float) -> None:
self._times[name] = self._times.get(name, 0) + val
def get_time_ms(self, name: str) -> int:
return int(self._times.get(name, 0) * 1000)
def aggregate(self, other: Self) -> None:
for key, val in other._times.items():
self._add_time(key, val)