C #-use the stopwatch class for Speed Testing

Source: Internet
Author: User
The running speed of the software must be within the acceptable range of users. Generally, improving the speed of short but frequently used routines greatly increases the overall speed of the software.
To improve the speed, you must first be able to measure the time. Well, let's take a look at the scene on the runway. When the gunshots hit, we immediately press the stopwatch to start timing and end timing when the contestant reaches the finish point, now you can know the time used by the contestant. To start timing the next round, you must first reset the stopwatch to zero .. 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. Make sure the application Program To meet users' expectations, we 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. Code Quality 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:


Using system;

Namespace stopwatchclass
{
Class Program
{
Static void main (string [] ARGs)
{

Long Total = 0;

For (INT I = 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:

Using system;
UsingSystem. diagnostics;

Namespace stopwatchclass
{
Class Program
{
Static void main (string [] ARGs)
{
Stopwatch Timer=NewStopwatch ();
Long Total = 0;

For (INT I = 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:

Using system;
Using system. diagnostics;

Namespace stopwatchclass
{
Class Program
{
Static void main (string [] ARGs)
{
Stopwatch timer = new stopwatch ();
Long Total = 0;

Timer. Start ();
For (INT I = 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:


Using system;
Using system. diagnostics;

Namespace stopwatchclass
{
Class Program
{
Static void main (string [] ARGs)
{
Stopwatch timer = new stopwatch ();
Long Total = 0;

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

Timer. Stop ();

DecimalMicro = 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.

 

from: http://www.cnblogs.com/star65225692/archive/2008/04/02/1135016.html?

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.