Friday 21 August 2009

Measuring elapsed time in C++ using QueryPerformanceCounter()

Yesterday we saw a primitive approach to getting the elapsed time, today we will have a look at the most popular and standard way of getting the elapsed time using QueryPerformanceCounter and QueryPerformanceFrequency approach.

Unlike the GetTickCount approach, which is not very reliable and does not have good resolution, this approach is quite reliable and has very good resolution, often better than a ms. The only problem with this approach used to be that in old systems it may not be very reliable. For example in some old OS (before XP sp2) in case of multiple processors present, and if the clocks of both the processors are not very well synchronised (doe to buggy hardware) then you can get different results each time the QueryPerformanceCounter call is made. Another problem with some chipsets with regards to their power saving is that the frequency changes while we use GetPerformanceFrequency call only once during the program. This can result in incorrect timing being returned. There were some other problems being present as well but they have now all seem to be fixed either in firmware or in the OS. It is recommended that this calls should only be used in OS greater than or equal to Windows XP SP2.




//Program tested on Microsoft Visual Studio 2008 - Zahid Ghadialy
//This program shows example of Getting Elapsed Time
#include <iostream>
#include <Windows.h>

using namespace
std;

LARGE_INTEGER timerFreq_;
LARGE_INTEGER counterAtStart_;

void
startTime()
{

QueryPerformanceFrequency(&timerFreq_);
QueryPerformanceCounter(&counterAtStart_);
cout<<"timerFreq_ = "<<timerFreq_.QuadPart<<endl;
cout<<"counterAtStart_ = "<<counterAtStart_.QuadPart<<endl;
TIMECAPS ptc;
UINT cbtc = 8;
MMRESULT result = timeGetDevCaps(&ptc, cbtc);
if
(result == TIMERR_NOERROR)
{

cout<<"Minimum resolution = "<<ptc.wPeriodMin<<endl;
cout<<"Maximum resolution = "<<ptc.wPeriodMax<<endl;
}

else

{

cout<<"result = TIMER ERROR"<<endl;
}
}


unsigned int
calculateElapsedTime()
{

if
(timerFreq_.QuadPart == 0)
{

return
-1;
}

else

{

LARGE_INTEGER c;
QueryPerformanceCounter(&c);
return
static_cast<unsigned int>( (c.QuadPart - counterAtStart_.QuadPart) * 1000 / timerFreq_.QuadPart );
}
}


int
main()
{

//Increasing the accuracy of Sleep to 1ms using timeBeginPeriod
timeBeginPeriod(1); //Add Winmm.lib in Project
unsigned int diffTime = 0, lastTime = 0, newTime = 0;
startTime();
lastTime = calculateElapsedTime();
cout<<"Start Time = "<<lastTime<<endl;

Sleep(100);
newTime = calculateElapsedTime();
diffTime = newTime - lastTime;
cout<<"Time after 100ms Sleep = "<<newTime<<", Difference = "<<diffTime<<endl;
lastTime = newTime;

Sleep(100);
newTime = calculateElapsedTime();
diffTime = newTime - lastTime;
cout<<"Time after 100ms Sleep = "<<newTime<<", Difference = "<<diffTime<<endl;
lastTime = newTime;

Sleep(5);
newTime = calculateElapsedTime();
diffTime = newTime - lastTime;
cout<<"Time after 5ms Sleep = "<<newTime<<", Difference = "<<diffTime<<endl;
lastTime = newTime;

Sleep(50);
newTime = calculateElapsedTime();
diffTime = newTime - lastTime;
cout<<"Time after 50ms Sleep = "<<newTime<<", Difference = "<<diffTime<<endl;

timeEndPeriod(1); //Must be called if timeBeginPeriod() was called
return 0;
}


The output is as follows:

2 comments:

  1. Thanks for posting this example. It was very helpful in understanding how to use the high resolution performance counter, and confirmed my suspicion that sleep() can't be trusted to return exactly when requested.

    ReplyDelete
  2. How does one deal with rollback? What is the max size of the counter?

    ReplyDelete