ValueStopwatch Struct
A lightweight value-type stopwatch for measuring elapsed time without heap allocations.
public readonly struct ValueStopwatchExample
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
| Name | Description |
|---|---|
| IsActive | Gets a value indicating whether this stopwatch instance has been initialized via ValueStopwatch.StartNew. |
IsActive
ValueStopwatch.csGets a value indicating whether this stopwatch instance has been initialized via ValueStopwatch.StartNew.
public bool IsActive { get; }Property value
Methods
| Name | Description |
|---|---|
| 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.csGets 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
ValueStopwatchinstance.
GetElapsedTime(long, long)
ValueStopwatch.csCalculates the elapsed time between two timestamp values.
Parameters
startTimestamplong- The starting timestamp obtained from
ValueStopwatch.GetTimestamp. endTimestamplong- The ending timestamp obtained from
ValueStopwatch.GetTimestamp.
Returns
TimeSpan A TimeSpan representing the elapsed time between the two timestamps.
GetTimestamp()
ValueStopwatch.csStartNew()
ValueStopwatch.csCreates and starts a new ValueStopwatch instance.
public static ValueStopwatch StartNew()Returns
ValueStopwatch A new ValueStopwatch that has been started.