Address: http://blog.csdn.net/lsmdiao0812/article/details/3173374
LARGE_INTEGER tima, timb;
QueryPerformanceCounter (& tima );
Programs that use the QueryPerformanceCounter function in Windows Server 2003 and Windows XP may be improperly executed.
QueryPerformanceCounter to accurately calculate the time of a Specific Row
QueryPerformanceCounter to accurately calculate the time of a Specific Row
// This program shows how to use QueryPerformanceCounter to accurately calculate the maximum row time
// Code
- LARGE_INTEGER m_liPerfFreq = {0 };
- // Obtain the CPU Performance per second (Tick)
- QueryPerformanceFrequency (& m_liPerfFreq );
- LARGE_INTEGER m_liPerfStart = {0 };
- QueryPerformanceCounter (& m_liPerfStart );
- For (int I = 0; I <100; I ++)
- Cout <I <endl;
- LARGE_INTEGER liPerfNow = {0 };
- // Calculate the current CPU running time
- QueryPerformanceCounter (& liPerfNow );
- Int time = (liPerfNow. QuadPart-m_liPerfStart.QuadPart) * 1000)/m_liPerfFreq.QuadPart );
- Char buffer [100];
- Sprintf (buffer, "bytes row time % d millisecond", time );
- Cout <buffer <endl;
The QueryPerformanceCounter () function returns the value of the high-precision performance counter, which can be time in subtle units. however, the minimum exact unit of QueryPerformanceCounter () timing is related to the system. Therefore, you must query the system to obtain the tick response frequency of QueryPerformanceCounter.
QueryPerformanceFrequency () provides this frequency value and returns the number of clicks per second.
The exact calculation time starts when QueryPerformanceCounter () is called for the first time.
Assume that the obtained LARGE_INTEGER is nStartCounter and the function is called again after a period of time,
Set nStopCounter.
The difference between the two is divided by the frequency of QueryPerformanceFrequency (), which is the number of seconds from the start to the end. because the time function itself consumes a very short amount of time, a small amount of time overhead is required. however, this overhead is generally ignored. the formula is as follows:
NStopCounter-nStartCounter
ElapsedTime = -------------------------------------overhead
Frequency
Double time = (nStopCounter. QuadPart-nStartCounter.QuadPart)/frequency. QuadPart
These two functions are accurate time functions provided by VC only for Windows 95 and later versions, and require computers to support accurate timers on hardware.
The following is a prototype of the QueryPerformanceFrequency () and QueryPerformanceCounter () functions:
BOOL QueryPerformanceFrequency(LARGE_INTEGER *lpFrequency); BOOL QueryPerformanceCounter(LARGE_INTEGER *lpCount);
The data type ARGE_INTEGER can be an 8-byte long integer or a combination of two 4-byte long integer values. The specific usage depends on whether the compiler supports 64-bit. This type is defined as follows:
Typedef union _ LARGE_INTEGER {struct {DWORD LowPart; // 4-byte integer LONG HighPart; // 4-byte integer}; LONGLONG QuadPart; // 8-byte integer} LARGE_INTEGER;
Before timing, call the QueryPerformanceFrequency () function to obtain the clock frequency of the machine's Internal timer, and then call the QueryPerformanceCounter () function before and after a strictly scheduled event, the precise time of the event calendar is calculated based on the difference between the two counts and the clock frequency. Run the following code to implement accurate timing within 1 ms:
- LARGE_INTEGER litmp;
- LONGLONG QPart1, QPart2;
- Double dfMinus, dfFreq, dfTim;
- QueryPerformanceFrequency (& litmp );
- DfFreq = (double) litmp. QuadPart; // obtain the counter clock frequency
- QueryPerformanceCounter (& litmp );
- QPart1 = litmp. QuadPart; // obtain the initial value.
- Do
- {
- QueryPerformanceCounter (& litmp );
- QPart2 = litmp. QuadPart; // get the abort Value
- DfMinus = (double) (QPart2-QPart1 );
- DfTim = dfMinus/dfFreq; // obtain the corresponding time value, in seconds
- } While (dfTim <0.001 );
Its timing error is no more than 1 microsecond, and its precision is related to machine configurations such as CPU. The following program is used to test the exact duration of the function Sleep (100:
- LARGE_INTEGER litmp;
- LONGLONG QPart1, QPart2;
- Double dfMinus, dfFreq, dfTim;
- QueryPerformanceFrequency (& litmp );
- DfFreq = (double) litmp. QuadPart; // obtain the counter clock frequency
- QueryPerformanceCounter (& litmp );
- QPart1 = litmp. QuadPart; // obtain the initial value.
- Sleep (100 );
- QueryPerformanceCounter (& litmp );
- QPart2 = litmp. QuadPart; // get the abort Value
- DfMinus = (double) (QPart2-QPart1 );
- DfTim = dfMinus/dfFreq; // obtain the corresponding time value, in seconds
Due to the error of the Sleep () function, each execution result of the preceding program has a slight error. The following code implements precise timing in 1 microsecond:
- LARGE_INTEGER litmp;
- LONGLONG QPart1, QPart2;
- Double dfMinus, dfFreq, dfTim;
- QueryPerformanceFrequency (& litmp );
- DfFreq = (double) litmp. QuadPart; // obtain the counter clock frequency
- QueryPerformanceCounter (& litmp );
- QPart1 = litmp. QuadPart; // obtain the initial value.
- Do
- {
- QueryPerformanceCounter (& litmp );
- QPart2 = litmp. QuadPart; // get the abort Value
- DfMinus = (double) (QPart2-QPart1 );
- DfTim = dfMinus/dfFreq; // obtain the corresponding time value, in seconds
- } While (dfTim <0.000001 );
Its timing error generally does not exceed 0.5 microseconds, and its accuracy is related to the configurations of machines such as CPU.