namespaces: System.Diagnostics
Stopwatch Instances can measure the elapsed time of a single time interval, or they can measure the total elapsed time of multiple time intervals. In a typical Stopwatch scenario, call the start method, then call the Stop method, and finally use the Elapsed property to check the run time.
The Stopwatch instance is either running or stopped, and isrunning can be used to determine the current state of the Stopwatch . Start the measurement run time using start , or stop to measure the elapsed time using stop . Query run time values through properties Elapsed,elapsedmilliseconds , or elapsedticks . You can query the run time properties when the instance is running or stopped. The run time property is steadily incremented during the Stopwatch run, and remains the same when the instance is stopped.
By default, the run time value of theStopwatch instance is equal to the sum of all measured intervals. The cumulative run time count is started each time the start is called, and the current interval measurement is ended each time the Stop is called, and the accumulated run time value is frozen. Use the Reset method to clear the cumulative elapsed time in an existing Stopwatch instance.
The Stopwatch counts the ticks in the base timer mechanism to measure elapsed time. If the installed hardware and operating system support counters for high-resolution performance, the Stopwatch class will use this counter to measure elapsed time, otherwise theStopwatch class will use system counters to measure elapsed time. Use the Frequency and ishighresolution fields to determine the accuracy and resolution of the Stopwatch timing implementation.
The Stopwatch class provides help for the operation of timing-related performance counters within managed code. Specifically, theFrequency field and the Gettimestamp method can be used to replace the unmanaged Win32 API queryperformancefrequency and QueryPerformanceCounter.
Attention |
On multiprocessor computers, it does not matter which processor the thread is running on. However, due to a bug in the BIOS or hardware abstraction layer (HAL), different timing results may be obtained on different processors. To specify processor affinity for a thread, use the processthread.processoraffinity method. |
C#_stopwatch class