PDA

View Full Version : Accurate time mesurement in Qt. (QueryPerformanceCounter equivalent?)



progDes
14th May 2009, 07:22
Hi all,

I need to mesure time from one moment to another.
Is there are any equivalent of WinAPI QueryPerformanceCounter()?

Needed accuracy is at least 20 ms. I could use QTime::start() and QTime::ellapsed() but this approach gives problems if someone change system time.

Probably I could use some function that returns time from the begining of runing the application. Wich would be good thing. But I cant find any function like this.

So I need robust and accurate time mesurement. Any ideas?

Thanks

wysota
14th May 2009, 08:59
You can have a QTimer that timeouts every 20ms and keeps track of the time itself but I'd say this is a bit too much of an effort. Why can't you use the native API function? Also you can take Qt's sources and see how Q_BENCHMARK macro is implemented - you can even borrow its code.

progDes
14th May 2009, 09:18
You can have a QTimer that timeouts every 20ms and keeps track of the time itself but I'd say this is a bit too much of an effort. Why can't you use the native API function? Also you can take Qt's sources and see how Q_BENCHMARK macro is implemented - you can even borrow its code.

The problems that there is no garantee that timer function will be called exactly every 20 ms. Some time will be lost on message-loop. And the main problem - system stress, when timer can return even in few seconds instead of 20 ms.

I can use platform specific API's but wanted to have one code for all platforms. Althought looks like it's not possible.

wysota
14th May 2009, 09:54
If you're not using a real-time OS then you will never have any guarantees.

And it doesn't matter if the timer fires after 20ms or 21ms, you have a QTime object to measure the time difference between timeouts. It won't be possible to use the same code for all platforms because each system has its own policy when it comes to timers (including different timer resolutions), so if you need something really precise, you'll have to dig into native API (although again without a RTOS it doesn't make much sense).

progDes
14th May 2009, 11:23
I've decided to use QueryPerformanceCounter in Windows. And mach_absolute_time oon MacOs. Both functions are actualy doing the same and can mesure time very precise.

hamidrsadeghi1
17th March 2011, 08:21
Maybe it's too late to reply, but I was looking for a substitute for QueryPerformanceCounter in Qt and found "QElapsedTimer" class new-added in Qt 4.7.

This class has it all :)