warg.decorators.timing.StopWatch¶
- class warg.decorators.timing.StopWatch(function: ~typing.Callable = <built-in function perf_counter>, auto_start_on_construction: bool = False, auto_start_on_enter: bool = True, auto_stop_on_exit: bool = True)[source]¶
Bases:
AbstractContextManagerMeasure execution time of function.
Can be used as context manager or function decorator, perform checkpoints or display absolute time from measurements beginning.
Used as context manager:
with Timer() as timer: ... # your operations logger.info(timer) # __str__ calls timer.time() internally timer.checkpoint() # register checkpoint ... # more operations logger.info(timer.checkpoint()) # time since last timer.checkpoint() call ... # even more operations logger.info(timer) # time taken for the block, will not be updated outside of it
When execution leaves the block, timer will be blocked. Last checkpoint and time taken to execute whole block will be returned by checkpoint() and time() methods respectively.
Used as function decorator:
@Timer() def foo(): return 42 value, time = foo()
- Parameters:
function (Callable, optional) – No argument function used to measure time. Default: time.perf_counter
- __init__(function: ~typing.Callable = <built-in function perf_counter>, auto_start_on_construction: bool = False, auto_start_on_enter: bool = True, auto_stop_on_exit: bool = True)[source]¶
Methods
__init__([function, ...])description
description
description
tick()Time taken since last tick call.
Attributes
Time taken since the start of timer (measurements beginning).
- property since_start¶
Time taken since the start of timer (measurements beginning).
- Returns:
Whatever self.function() - self.function() returns, usually fraction of seconds
- Return type:
time-like