_timer.py 417 B

12345678910111213141516171819
  1. """
  2. Timer context manager, only used in debug.
  3. """
  4. from time import time
  5. import contextlib
  6. from typing import Generator
  7. @contextlib.contextmanager
  8. def timer(subject: str = "time") -> Generator[None, None, None]:
  9. """print the elapsed time. (only used in debugging)"""
  10. start = time()
  11. yield
  12. elapsed = time() - start
  13. elapsed_ms = elapsed * 1000
  14. print(f"{subject} elapsed {elapsed_ms:.1f}ms")