2021-04-13 04:02:29 +00:00
|
|
|
"""
|
|
|
|
Timer context manager, only used in debug.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
from time import time
|
|
|
|
|
|
|
|
import contextlib
|
2022-01-24 04:07:52 +00:00
|
|
|
from typing import Generator
|
2021-04-13 04:02:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
@contextlib.contextmanager
|
2022-01-24 04:07:52 +00:00
|
|
|
def timer(subject: str = "time") -> Generator[None, None, None]:
|
2021-04-13 04:02:29 +00:00
|
|
|
"""print the elapsed time. (only used in debugging)"""
|
|
|
|
start = time()
|
|
|
|
yield
|
|
|
|
elapsed = time() - start
|
|
|
|
elapsed_ms = elapsed * 1000
|
|
|
|
print(f"{subject} elapsed {elapsed_ms:.1f}ms")
|