Get the exact time on windows

Source: Internet
Author: User

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

 

  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 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:

 
  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.