C # realizes measuring program running time and CPU usage time

Source: Internet
Author: User
Tags cpu usage

How do you want to count the number of packets per second that a server program can handle? The answer is to use the total number of packets processed, divided by the time it takes to process the packets. To point out here, the running of a program, the use of CPU time, and the actual running time is not the same. The bylaws are as follows:

private void ShowRunTime()
      {
      TimeSpan ts1 = Process.GetCurrentProcess().TotalProcessorTime;
      Stopwatch stw = new Stopwatch();
      stw.Start();
      int Circles = 1000;
      for (int i = 0; i < Circles; ++i)
      {
        Console.WriteLine(i.ToString());
      }
      double Msecs = Process.GetCurrentProcess().TotalProcessorTime.Subtract(ts1).TotalMilliseconds;
      stw.Stop();
      Console.WriteLine(string.Format("循环次数:{0} CPU时间(毫秒)={1} 实际时间(毫秒)={2}", Circles, Msecs, stw.Elapsed.TotalMilliseconds, stw.ElapsedTicks));
      Console.WriteLine(string.Format("1 tick = {0}毫秒", stw.Elapsed.TotalMilliseconds / stw.Elapsed.Ticks));
    }

The program output is as follows:

Cycles: 1000 CPU time (milliseconds) =50.072 actual time (ms) =666.9071

1 tick = 0.0001 ms

As can be seen in this example, the gap between the two is large, for the following reasons:

1 Windows is a multitasking operating system that allocates CPU time polling on a per-thread basis. That is, a program running halfway, may be stripped of CPU resources for other programs to run.

2 The program itself will not occupy CPU time waiting process. This wait may be the initiative of our program, such as starting a process, and then waiting for the end of the process, or we may not be aware of, such as the Console.WriteLine method of example, guess that its internal a series of asynchronous I/O operations and then wait for the completion of the operation, This does not occupy the CPU time of the calling process, but it consumes a lot of latency.

Summarize:

1 performance measurement, should use the program running time to measure, of course, also need to use CPU time as a reference, if the gap between the two is very large, need to consider why this situation.

2). NET Stopwatch class can be accurate to 1/10000 milliseconds, the basic can meet the measurement accuracy.

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.