Get elapsed time in Qt

C++Qt

C++ Problem Overview


I'm looking for the equivalent in Qt to GetTickCount()

Something that will allow me to measure the time it takes for a segment of code to run as in:

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

any suggestions?

C++ Solutions


Solution 1 - C++

I think it's probably better to use QElapsedTimer since that is why the class exists in the first place. It was introduced with Qt 4.7. Note that it is also immuned to system's clock time change.

Example usage:

#include <QDebug>
#include <QElapsedTimer>
...
...
QElapsedTimer timer;
timer.start();
slowOperation();  // we want to measure the time of this slowOperation()
qDebug() << timer.elapsed();

Solution 2 - C++

How about QTime? Depending on your platform it should have 1 millisecond accuracy. Code would look something like this:

QTime myTimer;
myTimer.start();
// do something..
int nMilliseconds = myTimer.elapsed();

Solution 3 - C++

Even if the first answer was accepted, the rest of the people who read the answers should consider sivabudh's suggestion.
QElapsedTimer can also be used to calculate the 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();

Solution 4 - C++

Expending the 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 simple use as:

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

output:

> onSpeedChanged : 102 Elapsed time: 2 ms.

Solution 5 - C++

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

For example, the following code run 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 output:

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

You should measure this for yourself and respect the overhead for your timing.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionshooshView Question on Stackoverflow
Solution 1 - C++sivabudhView Answer on Stackoverflow
Solution 2 - C++Dusty CampbellView Answer on Stackoverflow
Solution 3 - C++Lilian A. MoraruView Answer on Stackoverflow
Solution 4 - C++DamienView Answer on Stackoverflow
Solution 5 - C++Oliver HoffmannView Answer on Stackoverflow