"""This module contains utilities for tracking execution time."""from__future__importannotationsfromtimeimportperf_counterfromtypingimportno_type_check
[docs]@no_type_checkdeftimeit(func):"""Function to be used as decorator to time a function."""deftimed(*args,**kwargs):tic=perf_counter()result=func(*args,**kwargs)toc=perf_counter()print(f"{func.__name__}{(toc-tic)*1000:.2f} ms")returnresultreturntimed
[docs]classTimer:# pragma: no cover"""Timer class based on perf_counter."""def__init__(self)->None:"""Creates an instance of the class."""self._tic=perf_counter()self._toc:None|float=Noneself.paused=False
[docs]defresume(self)->None:"""Resume function."""ifnotself.paused:raiseValueError("Timer is not paused!")assertself._tocisnotNoneself._tic=perf_counter()-(self._toc-self._tic)self._toc=Noneself.paused=False