noether.core.utils.common.stopwatch

Classes

Stopwatch

A stopwatch class to measure elapsed time.

Module Contents

class noether.core.utils.common.stopwatch.Stopwatch(device=None)

A stopwatch class to measure elapsed time.

Supports two timing backends depending on the provided device:

  • CPU (device=None): plain time.perf_counter() wall-clock timing.

  • GPU (CUDA or MPS device): device event-based timing using torch.cuda.Event / torch.mps.Event for accurate GPU measurements. Events are recorded non-blocking in stop() / lap() and resolved lazily in elapsed_seconds, which synchronizes each pending event pair individually before calling elapsed_time().

Parameters:

device (torch.device | None) – Optional device that selects the timing backend.

start()

Start the stopwatch.

Return type:

Stopwatch

stop()

Stop the stopwatch and return the elapsed time since the last lap.

For GPU devices the return value is always float("nan"); access elapsed_seconds after stopping to obtain the resolved time.

Return type:

float

lap()

Record a lap time and return the elapsed time since the last lap.

For GPU devices the return value is always float("nan"); access elapsed_seconds after stopping to obtain the resolved time.

Return type:

float

static sync(device)

Synchronize the given GPU device. No-op for CPU devices.

Parameters:

device (torch.device)

Return type:

None

property last_lap_time: float

Return the last lap time.

Return type:

float

property lap_count: int

Return the number of laps recorded.

Return type:

int

property average_lap_time: float

Return the average lap time.

Return type:

float

property elapsed_seconds: float

Return the total elapsed time since the stopwatch was started.

For GPU stopwatches, flushes any pending event pairs first, synchronizing each end event before calling elapsed_time().

Return type:

float

property elapsed_milliseconds: float

Return the total elapsed time since the stopwatch was started in milliseconds.

Return type:

float