ValueStopwatch Struct

A lightweight value-type stopwatch for measuring elapsed time without heap allocations.

public readonly struct ValueStopwatch

Example

Measure execution time of an operation:

var stopwatch = ValueStopwatch.StartNew();

// Perform some operation
Thread.Sleep(10);

TimeSpan elapsed = stopwatch.GetElapsedTime();
Assert.True(elapsed > TimeSpan.Zero);

Properties

NameDescription
IsActiveGets a value indicating whether this stopwatch instance has been initialized via ValueStopwatch.StartNew.

Gets a value indicating whether this stopwatch instance has been initialized via ValueStopwatch.StartNew.

public bool IsActive { get; }

Property value

bool

Methods

NameDescription
GetElapsedTime()Gets the elapsed time since this stopwatch was started.
GetElapsedTime(long, long)Calculates the elapsed time between two timestamp values.
GetTimestamp()Gets the current timestamp value from the high-resolution performance counter.
StartNew()Creates and starts a new ValueStopwatch instance.

GetElapsedTime overloads

GetElapsedTime()

ValueStopwatch.cs

Gets the elapsed time since this stopwatch was started.

public TimeSpan GetElapsedTime()
Returns

TimeSpan A TimeSpan representing the elapsed time since the stopwatch was started.

Exceptions
InvalidOperationException
Thrown when called on an uninitialized or default ValueStopwatch instance.

GetElapsedTime(long, long)

ValueStopwatch.cs

Calculates the elapsed time between two timestamp values.

public static TimeSpan GetElapsedTime(long startTimestamp, long endTimestamp)
Parameters
startTimestamp long
The starting timestamp obtained from ValueStopwatch.GetTimestamp.
endTimestamp long
The ending timestamp obtained from ValueStopwatch.GetTimestamp.
Returns

TimeSpan A TimeSpan representing the elapsed time between the two timestamps.

GetTimestamp()

ValueStopwatch.cs

Gets the current timestamp value from the high-resolution performance counter.

public static long GetTimestamp()

Returns

long A long integer representing the current timestamp.

Creates and starts a new ValueStopwatch instance.

public static ValueStopwatch StartNew()

Returns

ValueStopwatch A new ValueStopwatch that has been started.