Stopwatch

class Stopwatch(timeSource: TimeSource.WithComparableMarks = TimeSource.Monotonic)

A stopwatch that accumulates elapsed time across start and stop cycles.

Time is read from timeSource, which defaults to TimeSource.Monotonic. The stopwatch starts out stopped with a zero elapsed value. Stopping keeps the accumulated time; use reset or restart to clear it.

This class is not thread-safe. Confine each instance to a single thread or coroutine, or guard it with external synchronization.

Constructors

Link copied to clipboard
constructor(timeSource: TimeSource.WithComparableMarks = TimeSource.Monotonic)

Properties

Link copied to clipboard

The total accumulated time, updating live while the stopwatch runs.

Link copied to clipboard

Whether the stopwatch is currently running.

Functions

Link copied to clipboard
inline fun <T> measure(block: () -> T): T

Runs block while the stopwatch is running and returns the block's result.

Link copied to clipboard
fun reset()

Stops the stopwatch and clears elapsed to Duration.ZERO.

Link copied to clipboard
fun restart()

Clears elapsed to Duration.ZERO and starts the stopwatch in one call.

Link copied to clipboard
fun start()

Starts the stopwatch, doing nothing when it is already running.

Link copied to clipboard
fun stop()

Stops the stopwatch and folds the running segment into elapsed, doing nothing when already stopped.