Solved: time measurement
Is there any function or method to calculate time in millisecond precisely in Qt? The application is to measure time between click of two targets.
Yes, but only for a given value of "precisely". A lovely class named "QTimer":http://qt-project.org/doc/qt-4.8/QTimer.html should be your friend here.
EDIT: Actually in your case, you should take a look at "QTime":http://qt-project.org/doc/qt-4.8/QTime.html.
[[doc:QElapsedTimer]] should be your friend. But: the precision is limited. What precision are you looking for exactly?
The precision around 1 ms is acceptable.
From the manual I quote: "QElapsedTimer will always use the same clock, so it's safe to compare with the value coming from another process in the same machine". May be it is appropriate for testing/evaluation process that is for my application. Is it okay?
Thanks for every reply...
I doubt you're going to get anywhere near 1ms resolution. Most systems don't provide a clock that fast (or accurate) by default, and then there is the way clicks are actually processed. They are picked up by QApplication, who creates QEvent instances that get posted to the event loop. From the event loop, they are processed by sending them to their targets one by one. That means that if you are already doing some work, or if one of the other events in the queue triggers a bit of work to be done, the delivery of your mouse event will be delayed. There goes your 1ms accuracy out of the window...
If your timing hardware is up to it (which is mostly the case nowadays), you can get up to microsecond accuracy with gettimeofday (linux) or QueryPerformanceCounter (Windows). But those aren't Qt functions, you'd need to write a wrapper yourself.
Note that Andres post basically tells you the limit in your application: the event queue system and thread time-slicing (~20ms) of the operating system. So the functions above are only of use to measure the time between the execution of the respective code lines, e.g. to measure performance, and not in your case to measure physical events that get queued by the OS before they reach you.
[quote author="DerManu" date="1350077601"]If your timing hardware is up to it (which is mostly the case nowadays), you can get up to microsecond accuracy with gettimeofday (linux) or QueryPerformanceCounter (Windows). But those aren't Qt functions, you'd need to write a wrapper yourself.[/quote]This is exactly what QElapsedTimer does.
I've had problems with the readings of QElapsedTimer, then I found out that the good old "clock()":http://www.cplusplus.com/reference/clibrary/ctime/clock/ function from the C standard library works very well if you don't need more than millisecond resolution. There might be implementations that return actual processor clocks but the ones I've used return miliseconds - mingW, MSVC, ICC. This is a good thing since modern processors change their running frequencies all the time and even if you had raw clocks there is no way to calculate the actual time those took. Back in the days of C processors were running on a fixed clock speed.
int start = clock();
... do some work
int time = clock() - start;@
time should be the time in msec
[quote author="Lukas Geyer" date="1350123216"]This is exactly what QElapsedTimer does.
Only since 4.8. In 4.7 it only provided millisecond accuracy, unfortunately.
Thanks for every suggestion and knowledge, my app connecting to openCV library too. I found cv::gettickcount and cv::gettickfrequency. The model is the same like utcenter's comment in C. I will explore more or may be someone already tried it?
It works well using utcenter's comment, better than using cv::gettickcount and cv::gettickfrequency in my case.