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: AbstractContextManager

Measure 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, ...])

override_arithmetics()

description

start_timer()

description

stop_timer()

description

tick()

Time taken since last tick call.

Attributes

since_start

Time taken since the start of timer (measurements beginning).

override_arithmetics()[source]

description

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

start_timer()[source]

description

stop_timer()[source]

description

tick()[source]

Time taken since last tick call.

If wasn’t called before, it is the same as as Timer creation time (first call returns the same thing as time())

Returns:

Whatever self.function() - self.function() returns, usually fraction of seconds

Return type:

time-like