Stopwatch timer, stopwatch C #

Source: Internet
Author: User
. Net2.0 also provides the stopwatch class, which can accurately measure the time.
Speed test:
The performance and testability of software is a complex topic. To ensure that an application meets your expectations, you need to consider its performance and testability during the development cycle. This is crucial in the design phase, and a poor design will almost certainly lead to a poor user experience. However, only a good design does not ensure that the program can run efficiently. The quality of the final code is equally important.
A routine that measures a long running time is quite simple. If a process lasts for several minutes, you only need a watch to record its time. For example, if the execution time is two minutes, 10% improvement can save 12 seconds, which is easy to determine.
If you want to measure a very short process, you need to consider better accuracy. For example, some small routines may only run for 1‰ seconds, but will be called for 1 million times, so the cumulative effect will be obvious. In. in earlier versions of the. NET Framework, you must use Windows API functions. in Net Framework 2.0, Microsoft introduced the stopwatch class to simplify time measurement tasks.
Stopwatch class:
It is very easy to use the stopwatch class to measure the time. Like the stopwatch in real life, this class object can also start, stop, and reset the counter, but it is much more accurate than the ordinary stopwatch, it can be accurate to microseconds (that is, one second per million ).
Sample Code:
To demonstrate how to use stopwatch, refer to the Code section. The following is a console application that accumulates All integers from 1 to 1 million:


Usingsystem;

Namespacestopwatchclass
{
Classprogram
{
Staticvoidmain (string [] ARGs)
{

Longtotal = 0;

For (INTI = 1;
I <= 10000000; I ++)
{
Total + = I;
}
}
}
}

Add a stopwatch object:
The stopwatch class is located in the system. Diagnostics namespace. The code for adding an object is as follows:

Usingsystem;
UsingSystem. diagnostics;

Namespacestopwatchclass
{
Classprogram
{
Staticvoidmain (string [] ARGs)
{
Stopwatch Timer= NewStopwatch ();
Longtotal = 0;

For (INTI = 1;
I <= 10000000; I ++)
{
Total + = I;
}
}
}
}

Control stopwatch objects:
Stopwatch provides several methods to control the stopwatch object. The start method starts a timing operation, and the Stop method stops timing. If the start method is used for the second time, the timing continues, and the final result is the accumulation of the two times. To avoid this, use the reset method to zero the object before the second timer. No parameters are required for these three methods. The code is:


Usingsystem;
Usingsystem. diagnostics;

Namespacestopwatchclass
{
Classprogram
{
Staticvoidmain (string [] ARGs)
{
Stopwatch timer = newstopwatch ();
Longtotal = 0;

Timer. Start ();
For (INTI = 1;
I <= 10000000; I ++)
{
Total + = I;
}

Timer. Stop ();
}
}
}

Read stopwatch results:
After the timing is completed, the next step is to read the timing result. The stopwatch class provides the following attributes:

  • Elapsed: returns a timespan object, indicating the time interval;
  • Elapsedmilliseconds: returns the number of microseconds after timing, with a slightly poor accuracy. It is suitable for timing a little longer;
  • Elapsedticks: returns the number of timer scales (timer tick) that have elapsed time. The timer scale is the smallest possible measurement unit of the stopwatch object. The length of the timer scale time is determined by the specific computer and operating system. The value of the Frequency Static Field of the stopwatch object indicates the number of timer scales contained in one second. Note the difference between it and the time unit used by the ticks attribute of timespan.

Select an Attribute Based on the timing task. In our example program, the elapsed attribute provides the required precision and uses it to output the number of milliseconds passed. This is also the highest precision of timespan.
The final program code is as follows:


Usingsystem;
Usingsystem. diagnostics;

Namespacestopwatchclass
{
Classprogram
{
Staticvoidmain (string [] ARGs)
{
Stopwatch timer = newstopwatch ();
Longtotal = 0;

Timer. Start ();
For (INTI = 1;
I <= 10000000; I ++)
{
Total + = I;
}

Timer. Stop ();

Decimal micro = timer. elapsed. ticks/10 m;
Console. writeline ("execution time was {0: F1} microseconds.", micro );
}
}
}

In addition, you can use the isrunning attribute to check whether a stopwatch instance is timing. You can use the startnew method to start a new timer.

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.