timer utility
This commit is contained in:
1 parent
7bfc02e683
commit
d80a3ae2b1
1 file changed
+19
@@ -6,6 +6,7 @@ if they are available and otherwise point to mocks.
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
import time
|
import time
|
||||||
|
from collections import defaultdict
|
||||||
import importlib.util
|
import importlib.util
|
||||||
import gzip
|
import gzip
|
||||||
|
|
||||||
@@ -74,6 +75,24 @@ def hr_time(seconds):
|
|||||||
return s
|
return s
|
||||||
|
|
||||||
|
|
||||||
|
class Timer:
|
||||||
|
def __init__(self): self.s = 0
|
||||||
|
def __enter__(self): self.start_time = time.perf_counter()
|
||||||
|
def __exit__(self, *args): self.s += time.perf_counter() - self.start_time
|
||||||
|
@property
|
||||||
|
def ms(self): return self.s*1e3
|
||||||
|
@property
|
||||||
|
def us(self): return self.s*1e6
|
||||||
|
def __repr__(self): return f'{self.s:.3f}s' if self.s >= 1 else f'{self.ms:.3f}ms'
|
||||||
|
|
||||||
|
|
||||||
|
class Timers:
|
||||||
|
def __init__(self): self.timers = defaultdict(Timer)
|
||||||
|
def __getitem__(self, name): return self.timers[name]
|
||||||
|
def __getattr__(self, name): return self.timers[name]
|
||||||
|
def __repr__(self): return ''.join([f'{k}={v} ' for k, v in self.timers.items()])
|
||||||
|
|
||||||
|
|
||||||
class Log:
|
class Log:
|
||||||
"""A very simple logger that formats the messages with the number of seconds since
|
"""A very simple logger that formats the messages with the number of seconds since
|
||||||
program start.
|
program start.
|
||||||
|
|||||||
Reference in new issue
Block a user