riogrande.timing#

Provides a simple context manager class for timing code execution and recording intermediate durations (laps).

Classes#

TimedTask

A context manager for measuring elapsed time of code blocks.

Module Contents#

class riogrande.timing.TimedTask[source]#

A context manager for measuring elapsed time of code blocks.

This class allows timing of an entire block of code and recording intermediate checkpoints (“laps”) within it. Internally uses time.perf_counter() for high-resolution timing.

See also

get_duration()

Return the total elapsed time.

new_lab()

Record an intermediate lap time.

Examples

>>> with TimedTask() as t:
...     do_work()
...     t.new_lab()
...     do_more_work()
...
>>> t.get_duration()
2.3512  # total seconds elapsed
>>> t.labs
[0.7543, 1.5969]  # lap times
__enter__()[source]#

Start the timer upon entering the context.

Returns:

The timer instance itself.

Return type:

TimedTask

__exit__(exc_type, exc_value, exc_tb)[source]#

Stop the timer upon exiting the context.

Records a final lap and stores the total elapsed time between start and stop.

get_duration()[source]#

Get the total duration measured between start and stop.

Returns:

Total elapsed time in seconds.

Return type:

float

See also

new_lab()

Record an intermediate lap time.

new_lab()[source]#

Record a new lap (checkpoint).

Each call appends the time elapsed since the previous lap (or since the start if it’s the first lap).

See also

get_duration()

Return the total elapsed time.

labs = [][source]#