free hit counter

Wednesday, December 08, 2010

precision timers

Trying to timestamp when USB data is received and a standard QTimer was terrible. It turns out under Windows you can get something more accurate:
QueryPerformanceCounter() solves the issue.

Sample code below for getting out the timer tick in milliseconds. I should really put this into a nice little object, but no time right now!


...
QueryPerformanceFrequency(&_TICKS_TO_MSEC);
qDebug() << _TICKS_TO_MSEC.QuadPart;
_TICKS_TO_MSEC.QuadPart= _TICKS_TO_MSEC.QuadPart / 1000;
qDebug() << _TICKS_TO_MSEC.QuadPart;
QueryPerformanceCounter(&_resetTime);
getTimeMs(); //!!!
getTimeMs(); //!!!
QTimer::singleShot(1000, this, SLOT(getTimeMs()));
...
double getTimeMs()
{
double result;

LARGE_INTEGER ticks;
QueryPerformanceCounter(&ticks);

ticks.QuadPart= ticks.QuadPart - _resetTime.QuadPart;
result= ((double)ticks.QuadPart) / ((double)_TICKS_TO_MSEC.QuadPart) ;

qDebug() << "tstamp: " << result;

return result;
}


Returns these results, showing that QTimer is accurate within 10ms.

2337968
2337
tstamp: 0.0012837
tstamp: 0.132221
tstamp: 1010.78

Labels:

0 Comments:

Post a Comment

<< Home