VC ++ precisely obtains the time (queryperformancecounter)

Source: Internet
Author: User
Tags integer numbers

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

 

  1. Large_integer m_liperffreq = {0 };
  2. // Obtain the CPU performance per second (tick)
  3. Queryperformancefrequency (& m_liperffreq );
  4. Large_integer m_liperfstart = {0 };
  5. Queryperformancecounter (& m_liperfstart );
  6. For (INT I = 0; I <100; I ++)
  7. Cout <I <Endl;
  8. Large_integer liperfnow = {0 };
  9. // Calculate the current CPU running time
  10. Queryperformancecounter (& liperfnow );
  11. Int time = (liperfnow. quadpart-m_liperfstart.quadpart) * 1000)/m_liperffreq.quadpart );
  12. Char buffer [100];
  13. Sprintf (buffer, "bytes row time % d millisecond", time );
  14. 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:

 
  1. Large_integer litmp;
  2. Longlong qpart1, qpart2;
  3. Double dfminus, dffreq, dftim;
  4. Queryperformancefrequency (& litmp );
  5. Dffreq = (double) litmp. quadpart; // obtain the counter clock frequency
  6. Queryperformancecounter (& litmp );
  7. Qpart1 = litmp. quadpart; // obtain the initial value.
  8. Do
  9. {
  10. Queryperformancecounter (& litmp );
  11. Qpart2 = litmp. quadpart; // get the abort Value
  12. Dfminus = (double) (QPart2-QPart1 );
  13. Dftim = dfminus/dffreq; // obtain the corresponding time value, in seconds
  14. } 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:

 
  1. Large_integer litmp;
  2. Longlong qpart1, qpart2;
  3. Double dfminus, dffreq, dftim;
  4. Queryperformancefrequency (& litmp );
  5. Dffreq = (double) litmp. quadpart; // obtain the counter clock frequency
  6. Queryperformancecounter (& litmp );
  7. Qpart1 = litmp. quadpart; // obtain the initial value.
  8. Sleep (100 );
  9. Queryperformancecounter (& litmp );
  10. Qpart2 = litmp. quadpart; // get the abort Value
  11. Dfminus = (double) (QPart2-QPart1 );
  12. 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:

 
  1. Large_integer litmp;
  2. Longlong qpart1, qpart2;
  3. Double dfminus, dffreq, dftim;
  4. Queryperformancefrequency (& litmp );
  5. Dffreq = (double) litmp. quadpart; // obtain the counter clock frequency
  6. Queryperformancecounter (& litmp );
  7. Qpart1 = litmp. quadpart; // obtain the initial value.
  8. Do
  9. {
  10. Queryperformancecounter (& litmp );
  11. Qpart2 = litmp. quadpart; // get the abort Value
  12. Dfminus = (double) (QPart2-QPart1 );
  13. Dftim = dfminus/dffreq; // obtain the corresponding time value, in seconds
  14. } 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

 

  1. Large_integer m_liperffreq = {0 };
  2. // Obtain the CPU performance per second (tick)
  3. Queryperformancefrequency (& m_liperffreq );
  4. Large_integer m_liperfstart = {0 };
  5. Queryperformancecounter (& m_liperfstart );
  6. For (INT I = 0; I <100; I ++)
  7. Cout <I <Endl;
  8. Large_integer liperfnow = {0 };
  9. // Calculate the current CPU running time
  10. Queryperformancecounter (& liperfnow );
  11. Int time = (liperfnow. quadpart-m_liperfstart.quadpart) * 1000)/m_liperffreq.quadpart );
  12. Char buffer [100];
  13. Sprintf (buffer, "bytes row time % d millisecond", time );
  14. 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:

 
  1. Large_integer litmp;
  2. Longlong qpart1, qpart2;
  3. Double dfminus, dffreq, dftim;
  4. Queryperformancefrequency (& litmp );
  5. Dffreq = (double) litmp. quadpart; // obtain the counter clock frequency
  6. Queryperformancecounter (& litmp );
  7. Qpart1 = litmp. quadpart; // obtain the initial value.
  8. Do
  9. {
  10. Queryperformancecounter (& litmp );
  11. Qpart2 = litmp. quadpart; // get the abort Value
  12. Dfminus = (double) (QPart2-QPart1 );
  13. Dftim = dfminus/dffreq; // obtain the corresponding time value, in seconds
  14. } 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:

 
  1. Large_integer litmp;
  2. Longlong qpart1, qpart2;
  3. Double dfminus, dffreq, dftim;
  4. Queryperformancefrequency (& litmp );
  5. Dffreq = (double) litmp. quadpart; // obtain the counter clock frequency
  6. Queryperformancecounter (& litmp );
  7. Qpart1 = litmp. quadpart; // obtain the initial value.
  8. Sleep (100 );
  9. Queryperformancecounter (& litmp );
  10. Qpart2 = litmp. quadpart; // get the abort Value
  11. Dfminus = (double) (QPart2-QPart1 );
  12. 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:

 
  1. Large_integer litmp;
  2. Longlong qpart1, qpart2;
  3. Double dfminus, dffreq, dftim;
  4. Queryperformancefrequency (& litmp );
  5. Dffreq = (double) litmp. quadpart; // obtain the counter clock frequency
  6. Queryperformancecounter (& litmp );
  7. Qpart1 = litmp. quadpart; // obtain the initial value.
  8. Do
  9. {
  10. Queryperformancecounter (& litmp );
  11. Qpart2 = litmp. quadpart; // get the abort Value
  12. Dfminus = (double) (QPart2-QPart1 );
  13. Dftim = dfminus/dffreq; // obtain the corresponding time value, in seconds
  14. } 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.

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.