Program run time statistics (high accuracy)

Source: Internet
Author: User

1 need to obtain the system precise clock function:
1) for the general real-time control, the use of GetTickCount () function can meet the accuracy requirements, but to further improve the timing accuracy, it is necessary to use the QueryPerformanceFrequency () function and QueryPerformanceCounter ( function
2) These two functions are high-precision time functions provided by the VC for Windows 9X only, and require the computer to support high-precision timers from hardware. 3) The prototype of the QueryPerformanceFrequency () function and the QueryPerformanceCounter () function is:
BOOL QueryPerformanceFrequency (Large_integer *lpfrequency);
BOOL QueryPerformanceCounter (Large_integer *lpcount);
The data type Large-integer can be either a 8-byte integer or a union structure that is a two 4-byte integer, depending on whether the compiler supports 64-bit. This type is defined as follows:
typedef Union _LARGE_INTEGER {
struct {
DWORD LowPart;   4-byte integer number LONG highpart; 4-byte integer};
Longlong QuadPart;
8-byte integer number
} Large_integer;
4) Before timing, you should call the QueryPerformanceFrequency () function to get the clock frequency of the internal timer of the machine. I use this function on three kinds of pentiumⅱ machines to get the clock frequency is 1193180Hz. Then, the author calls the QueryPerformanceCounter () function before and after the event that requires strict timing, and calculates the exact time of the event experience by using the difference between the counts and the clock frequency obtained two times.
The following procedure is used to test the exact duration of the function sleep (100).
Large-integer litmp; Longlong Qpart1,qpart2;
Double Dfminus, Dffreq, Dftim;     QueryPerformanceFrequency (&LITMP); Get the clock frequency of the counter
Dffreq = (double) litmp.     QuadPart;     QueryPerformanceCounter (&LITMP); Get the initial value
QPart1 = litmp. QuadPart;

Sleep (100);
QueryPerformanceCounter (&LITMP); Get termination value
QPart2 = litmp. QuadPart;
Dfminus = (double) (QPART2-QPART1);   Dftim = Dfminus/dffreq; Get the corresponding time value
Execute the above program and get the result of dftim=0.097143767076216 (seconds). Careful readers will find that the result of each execution is different, there is a certain difference, this is due to sleep () of its own error.

Program run time statistics (high 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.