perturbopy.postproc.utils.timing.Timing

class perturbopy.postproc.utils.timing.Timing(tag, level=0)

Bases: object

Class to measure timing for a specific task.

Parameters:

tag (str) – The tag or label for the timing measurement.

tag

The label for the timing measurement.

Type:

str

t_start

The start time of the timing measurement.

Type:

float or None

t_end

The end time of the timing measurement.

Type:

float or None

t_delta

The time difference between the start and end times.

Type:

float or None

total_runtime

The accumulated total runtime for multiple measurements.

Type:

float

call_count

The number of times the timing has been measured.

Type:

int

level

The level of importance (hierarchy) for the timing measurement.

Type:

int

start()

Start the timing measurement.

stop()

Stop the timing measurement and calculate the time difference.

__enter__()

Enter the context for the timing measurement.

__exit__(exc_type, exc_val, exc_tb)

Exit the context for the timing measurement.

__str__()

Return a string representation of the timing measurement.

Examples

# Example 1: Timing a code block using ‘with’ statement >>> with Timing(‘test’) as t: … # Code to be timed … time.sleep(1) … >>> print(t) test (s): 1.000

# Example 2: Manually timing a code block without ‘with’ statement >>> t = Timing(‘test’) >>> t.start() >>> # Code to be timed >>> time.sleep(1) >>> t.stop() >>> print(t) test (s): 1.000

# Example 3: Accumulating multiple timing measurements >>> t = Timing(‘test’) >>> t.start() >>> # Code to be timed >>> time.sleep(1) >>> t.stop() >>> t.start() >>> # Code to be timed again >>> time.sleep(0.5) >>> t.stop() >>> print(t) test (s): 1.500

Methods

start

stop