From: http://blog.csdn.net/daoyuly/archive/2009/03/01/3947918.aspx
Accurate Time timing is sometimes necessary. For example, the time synchronization between the video and the audio during multimedia playback, and the time must be used to test the code performance. Accurate Time timing is also required to test the performance of hardware. In this case, you need to use queryperformancecounter to query the counter value of the timer. If there is a timer in the hardware, it will start the timer and continuously obtain the value of the timer, it is as accurate as the crystal oscillator of the hardware clock.
Queryperformancecounter query performance counters
The queryperformancecounter function retrieves the current value of the high-resolution performance counter, if one exists.
This function is used to obtain precise performance counter values, if any.
Bool queryperformancecounter (
Large_integer * lpperformancecount // address of current counter value address of the current counter value
);
Parameters
Lpperformancecount
Points to a variable that the function sets, in counts, to the current performance-counter value. if the installed hardware does not support a high-resolution performance counter, this parameter can be to zero.
Pointer, pointing to a variable set by the function (generally a reference, Translator's note), used to return the value of the performance counter. if the installed hardware does not support high-precision performance counters, this parameter can be set to 0 (what is the significance of the call for querying ?).
Return values
If the installed hardware supports a high-resolution performance counter, the return value is nonzero.
Not supported. The return value is not 0.
If the installed hardware does not support a high-resolution performance counter, the return value is zero.
Otherwise, 0 is returned.
The following is an example:
Winbaseapi
Bool
Winapi
Queryperformancecounter (
_ Out large_integer * lpperformancecount
);
Winbaseapi
Bool
Winapi
Queryperformancefrequency (
_ Out large_integer * lpfrequency
);
Lpperformancecount is the current Count value of the timer.
Queryperformancefrequency is the frequency at which the timer is returned.
An example of calling a function is as follows:
// Precise clock query.
Void testhightimer (void)
{
//
Large_integer nfreq;
Large_integer nlasttime1;
Large_integer nlasttime2;
// Obtain whether a precise timer is supported.
If (queryperformancefrequency (& nfreq ))
{
//
Const int nbufsize = 256;
Tchar chbuf [nbufsize];
// Display the timer frequency.
Wsprintf (chbuf, _ T ("lasttime = % i64d \ r \ n"), nfreq );
Outputdebugstring (chbuf );
// Obtain the timer value.
Queryperformancecounter (& nlasttime1 );
Wsprintf (chbuf, _ T ("lasttime = % i64d \ r \ n"), nlasttime1 );
Outputdebugstring (chbuf );
Sleep (0 );
// Obtain the timer value.
Queryperformancecounter (& nlasttime2 );
Wsprintf (chbuf, _ T ("lasttime = % i64d \ r \ n"), nlasttime2 );
Outputdebugstring (chbuf );
// Calculate how many seconds it takes.
Float finterval = nlasttime2.quadpart-nlasttime1.quadpart;
Swprintf (chbuf, nbufsize, _ T ("cost: % F \ r \ n"), finterval/(float) nfreq. quadpart );
Outputdebugstring (chbuf );
}
}