3 """A simple stopwatch decorator / context for timing things."""
7 from typing import Callable, Literal
10 class Timer(contextlib.AbstractContextManager):
12 A stopwatch to time how long something takes (walltime).
16 with stopwatch.Timer() as t:
20 print(f'That took {walltime} seconds.')
23 def __init__(self) -> None:
27 def __enter__(self) -> Callable[[], float]:
28 """Returns a functor that, when called, returns the walltime of the
31 self.start = time.perf_counter()
33 return lambda: self.end - self.start
35 def __exit__(self, *args) -> Literal[False]:
36 self.end = time.perf_counter()