C # _ Stopwatch class,
Namespace:System. Diagnostics
StopwatchAn instance can measure the running time at one interval or the total running time at multiple intervals. In the typicalStopwatchIn the solution, first call the Start method, then call the Stop method, and then use the Elapsed attribute to check the running time.
StopwatchThe instance is running or stopped. You can confirm it by using IsRunning.Stopwatch. UseStartYou can start measuring the running time.StopYou can stop the measurement run time. Pass attributesElapsed, ElapsedMilliseconds, or ElapsedTicks. You can query the running time attribute when the instance is running or stopped. Run Time attribute inStopwatchIt increases steadily during running; it remains unchanged when the instance is stopped.
By default,StopwatchThe running time value of the instance is equivalent to the total time interval of all measurements. Each callStartTotal running time count; each callStopEnd the current interval measurement, and freeze the cumulative running time value. You can use the Reset method to clear existingStopwatchThe cumulative running time in the instance.
StopwatchIn the Basic timer mechanism, the timer scale is counted to measure the running time. If the installed hardware and Operating System Support counters with high-resolution performanceStopwatchClass will use this counter to measure the running time; otherwise,StopwatchClass will use the system counter to measure the running time. Use the Frequency and IsHighResolution fields to determine the implementation.StopwatchTiming accuracy and resolution.
StopwatchClass helps you manage timing-related performance counters in your code. Specifically,FrequencyThe field and GetTimestamp methods can be used to replace unmanaged Win32 APIs.QueryPerformanceFrequencyAndQueryPerformanceCounter.
Note: |
On a multi-processor computer, it does not matter on which processor the thread runs. However, due to a bug in the BIOS or hardware abstraction layer (HAL), different timing results may be obtained on different processors. To specify a processor association for a thread, use the ProcessThread. ProcessorAffinity method. |