Browse Source

timer utility

devel
Stefan Holst 2 years ago
parent
commit
d80a3ae2b1
  1. 19
      src/kyupy/__init__.py

19
src/kyupy/__init__.py

@ -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.

Loading…
Cancel
Save