Elapsed time in Qt

I am looking for an equivalent in Qt before GetTickCount()

Something that will allow me to measure the time it takes to execute a code segment:

 uint start = GetTickCount(); // do something.. uint timeItTook = GetTickCount() - start; 

any suggestions?

+59
c ++ qt
Oct 28 '08 at 20:02
source share
6 answers

What about QTime ? Depending on your platform, it should have an accuracy of 1 millisecond. The code will look something like this:

 QTime myTimer; myTimer.start(); // do something.. int nMilliseconds = myTimer.elapsed(); 
+78
Oct 28 '08 at 20:13
source share

I think it is better to use QElapsedTimer , since that is why the class exists. It was introduced with Qt 4.7. Note that it will also be affected by a change in system time.

Usage example:

 #include <QDebug> #include <QElapsedTimer> ... ... QElapsedTimer timer; timer.start(); slowOperation(); // we want to measure the time of this slowOperation() qDebug() << timer.elapsed(); 
+103
Dec 07 '10 at 21:23
source share

Even if the first answer has been accepted, the rest of the people who read the answers should consider the sivabudh proposal.
QElapsedTimer can also be used to calculate time in nanoseconds.
Code example:

 QElapsedTimer timer; qint64 nanoSec; timer.start(); //something happens here nanoSec = timer.nsecsElapsed(); //printing the result(nanoSec) //something else happening here timer.restart(); //some other operation nanoSec = timer.nsecsElapsed(); 
+34
Oct. 21 '12 at 23:31
source share

The general strategy is to invoke the observed method several times. 10 calls provide accuracy of 1.5 ms, 100 is one of 0.15 ms.

+1
Oct 26
source share

If you want to use QElapsedTimer , you must consider the overhead of this class.

For example, the following code runs on my machine:

 static qint64 time = 0; static int count = 0; QElapsedTimer et; et.start(); time += et.nsecsElapsed(); if (++count % 10000 == 0) qDebug() << "timing:" << (time / count) << "ns/call"; 

gives me this result:

 timing: 90 ns/call timing: 89 ns/call ... 

You must measure this for yourself and evaluate the overhead for your time.

+1
Mar 31 '14 at 21:08
source share

Commonplace previous answers, here is a macro that does everything for you.

 #include <QDebug> #include <QElapsedTimer> #define CONCAT_(x,y) x##y #define CONCAT(x,y) CONCAT_(x,y) #define CHECKTIME(x) \ QElapsedTimer CONCAT(sb_, __LINE__); \ CONCAT(sb_, __LINE__).start(); \ x \ qDebug() << __FUNCTION__ << ":" << __LINE__ << " Elapsed time: " << CONCAT(sb_, __LINE__).elapsed() << " ms."; 

And then you can just use like:

 CHECKTIME( // any code for (int i=0; i<1000; i++) { timeConsumingFunc(); } ) 

exit:

onSpeedChanged: 102 Elapsed time: 2 ms.

+1
Jul 29 '16 at 5:46
source share



All Articles