3 """A simple stopwatch decorator / context for timing things."""
6 from typing import Callable, Optional
11 A stopwatch to time how long something takes (walltime).
15 with stopwatch.Timer() as t:
19 print(f'That took {walltime} seconds.')
22 def __init__(self) -> None:
26 def __enter__(self) -> Callable[[], float]:
27 """Returns a functor that, when called, returns the walltime of the
30 self.start = time.perf_counter()
32 return lambda: self.end - self.start
34 def __exit__(self, *args) -> Optional[bool]:
35 self.end = time.perf_counter()
36 return None # don't suppress exceptions