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)