midgard.dev.timer
Class for timing the running time of functions and code blocks
Description:
The dev.timer
can be used to log the running time of functions and general
code blocks. Typically, you will import the Timer
-class from within the
module:
from midgard.dev.timer import Timer
The Timer can then be used in three different ways:
-
As a decorator to time one function:
@Timer('The time to execute some_function was') def some_function(some_argument, some_other_argument=some_value): pass
-
As a context manager together with
with
to time a code block:with Timer('Finish doing stuff in', logger=logger.debug) as t: do_something() do_something_else()
-
With explicit
start
- andend
-statements:t = Timer() t.start() do_something() do_something_else() t.end()
As can be seen in the examples above, Timer()
may be called with several
optional parameters, including the text to report when the timer ends and which
logger is used to report the timing. See Timer.__init__
for more details.
AccumulatedTimer
AccumulatedTimer(text:str='Elapsed time:', fmt:str='.4f', logger:Union[Callable[[str], NoneType], NoneType]=<built-in function print>) -> None
AccumulatedTimer.elapsed()
elapsed(self) -> float
Log the time elapsed
Can be used explicitly to log the time since a timer started without ending the timer.
Returns:
The time elapsed in seconds.
AccumulatedTimer.end()
end(self) -> float
End the timer and log the time elapsed
Returns:
The time elapsed in seconds.
AccumulatedTimer.pause()
pause(self) -> float
Pause the timer without logging. Use .start() to restart the timer
AccumulatedTimer.reset()
reset(self) -> None
Reset the timer back to 0
AccumulatedTimer.start()
start(self) -> None
Start the timer
AccumulatedTimer.timer()
timer() -> float
Get current value of timer
Using the built-in time.perf_counter
to do the timing.
Returns:
Current value of timer.
Timer
Timer(text:str='Elapsed time:', fmt:str='.4f', logger:Union[Callable[[str], NoneType], NoneType]=<built-in function print>) -> None
Class for timing running time of functions and code blocks.
Timer.elapsed()
elapsed(self) -> float
Log the time elapsed
Can be used explicitly to log the time since a timer started without ending the timer.
Returns:
The time elapsed in seconds.
Timer.end()
end(self) -> float
End the timer and log the time elapsed
Returns:
The time elapsed in seconds.
Timer.pause()
pause(self) -> float
Pause the timer without logging. Use .start() to restart the timer
Timer.start()
start(self) -> None
Start the timer
Timer.timer()
timer() -> float
Get current value of timer
Using the built-in time.perf_counter
to do the timing.
Returns:
Current value of timer.