c#-use stopwatch class for Speed test

Source: Internet
Author: User

The running speed of the software must be within the acceptable range of the user. Generally, improving the speed of those short but frequently used routines can significantly improve the overall speed of the software.

To improve speed, of course, we must first be able to measure time. OK, so let's think about the situation on the runway, the sound of the gunshot, immediately press the stopwatch to start the time, the player reached the end of the moment when the end of the timer, then you can know that the player to spend. To start the next round of time before the time, you have to zero the stopwatch first. NET2.0 also provides such a stopwatch: The Stopwatch class, which can measure time more accurately.

Speed test:

The performance and testability of software is a complex subject. To ensure that your application meets the expectations of your users, you need to consider its performance and testability during the development cycle. This is critical in the design phase, and a bad design can almost certainly lead to a bad user experience. However, just because a good design does not guarantee that the program will run efficiently, the quality of the final code is equally important.

It is fairly straightforward to measure a long-running routine. If a process lasts a few minutes, just a watch can record its time. For example, an execution time of two minutes of the process, 10% of the improvement can save 12 seconds, this is very easy to determine.

And if you want to measure a very short process, consider better accuracy. For example, there are some very small routines, they may run only 1 per thousand seconds, but will be called 1 million times, the cumulative effect is obvious. In previous versions of the. NET Framework, Windows API functions were required, and in the. NET Framework 2.0, Microsoft introduced the stopwatch (which is our stopwatch) class to simplify time measurement tasks.

Stopwatch class:

Using the Stopwatch class to measure time is simple. Like the stopwatch in real life, the object of this class can also start, stop, and Zero (reset) the counter, but it is much more accurate than the average stopwatch, which can be accurate to microseconds (that is, one out of 10,000 seconds).

Sample code:

To demonstrate the use of stopwatch or to come to a piece of code. Here is a console application that adds up 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;
         }
      }
    }
  }

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.