C # Measurement program run time and CPU Usage Time instance method

Source: Internet
Author: User

Private voidShowruntime () {TimeSpan ts1=process.getcurrentprocess ().   Totalprocessortime; Stopwatch STW=NewStopwatch (); StW.  Start (); intCircles = +;  for(inti =0; i < Circles; ++i) {}DoubleMsecs =process.getcurrentprocess (). Totalprocessortime.subtract (TS1).  TotalMilliseconds; StW.    Stop (); Console.WriteLine (string. Format ("number of cycles: {0} CPU time (ms) ={1} actual time (ms) ={2}", Circles, Msecs, STW. Elapsed.totalmilliseconds, STW.  Elapsedticks)); Console.WriteLine (string. Format ("1 tick = {0} milliseconds", STW. Elapsed.totalmilliseconds/STW. Elapsed.ticks)); }

The program output is as follows:
Cycles: CPU Time (ms) =50.072 actual time (ms) =666.9071
1 tick = 0.0001 ms

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

1) Windows is a multi-tasking operating system that polls CPU time on a per-thread basis. That is, a program running Midway, may be deprived of CPU resources for other programs to run.
2) The program itself will have a wait process that does not consume CPU time. This wait may be our program initiative, such as starting a process, and then waiting for the end of the process, or we are not aware of, such as the example of the Console.WriteLine method, guess 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 waiting time.
Summarize:
1) Performance measurement, should be measured with the program run time, of course, also need to use CPU time as a reference, if the gap 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.


Personal Grooming :
The elapsed and Elapsedmilliseconds properties, when called, internally invoke the QueryPerformanceFrequency () function in kernel32 to get the frequency of the high-precision timers that the processor supports. This number is then used to correct the obtained time difference, and the last elapsed property returns a new TimeSpan object. This high-precision calculation (fixup) is not performed when the Elapsedticks property is obtained. But in general the error is not very large. It is recommended to use the elapsed and Elapsedmilliseconds properties if the exact calculation time is required or if the amount of test data is particularly large.

There are questions :
After my test, I found that I had not answered the original author, as I have said. The friend who wants to know the reason is indicated below. Thank you.
From an internal implementation, Stopwatch.ishighresolution = True, StopWatch.Elapsed.Ticks should be equal to stopwatch.elapsedticks multiplied by frequency (constant of frequency). Conversely, these 2 numbers should be equal when isrunning is true. So it should be that StopWatch.Elapsed.Ticks will be more big.

C # Measurement program run time and CPU Usage Time instance method

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.