3 # © Copyright 2021-2023, Scott Gasch
5 """A simple stopwatch decorator / context for timing things. This was factored out
6 of decorator utils so that bootstrap.py can keep its imports lighter."""
10 from typing import Callable, Literal
13 class Timer(contextlib.AbstractContextManager):
15 A stopwatch to time how long something takes (walltime).
19 with stopwatch.Timer() as t:
23 print(f'That took {walltime} seconds.')
26 def __init__(self) -> None:
30 def __enter__(self) -> Callable[[], float]:
31 """Returns a functor that, when called, returns the walltime of the
34 self.start = time.perf_counter()
36 return lambda: self.end - self.start
38 def __exit__(self, *args) -> Literal[False]:
39 self.end = time.perf_counter()