For scheduled operations with high precision requirements, the queryperformancefrequency () and queryperformancecounter () functions should be used. 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. The timing process is as follows: Before timing, call the queryperformancefrequency () function to obtain the clock frequency of the machine's Internal timer, and then call queryperformancecounter () before and after a strictly scheduled event occurs () the function calculates the precise time of the event calendar based on the difference between the two counts and the clock frequency. 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 union structure of two 4-byte long integer numbers,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,Then, call the queryperformancecounter () function before and after a strictly scheduled event. Calculate the eventThe exact time of the calendar. 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.
For scheduled operations with high precision requirements, the queryperformancefrequency () and queryperformancecounter () functions should be used. 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. The timing process is as follows: Before timing, call the queryperformancefrequency () function to obtain the clock frequency of the machine's Internal timer, and then call queryperformancecounter () before and after a strictly scheduled event occurs () the function calculates the precise time of the event calendar based on the difference between the two counts and the clock frequency. 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 union structure of two 4-byte long integer numbers,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,Then, call the queryperformancecounter () function before and after a strictly scheduled event. Calculate the eventThe exact time of the calendar. 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.