C#_stopwatch class

Source: Internet
Author: User

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 APIqueryperformancefrequency 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.