How to measure function running time in Qt?

Bobur picture Bobur · Dec 15, 2016 · Viewed 10.8k times · Source

I am calling argon2 - memory intensive hashing function in Qt and measuring its running time:

...
QTime start = QTime::currentTime();
// call hashing function
QTime finish = QTime::currentTime();
time = start.msecsTo(finish) / 1000.0;
...

In argon2 library's test case, time is measured in another way:

...
clock_t start = clock();
// call hashing function
clock_t finish = clock();
time = ((double)finish - start) / CLOCKS_PER_SEC;
...

I am calling the function exactly as they call in their test case. But I am getting a twice bigger number (twice slower). Why? How to measure function running time in Qt? What clock() actually measures?

env:virtualBox, Ubuntu14.04 64bit, Qt5.2.1, Qt Creator 3.0.1.

Answer

Tom Conijn picture Tom Conijn · Dec 15, 2016

You could also try to use the QElapsedTimer:

QElapsedTimer timer;
timer.start();

slowOperation1();

qDebug() << "The slow operation took" << timer.elapsed() << "milliseconds";
qDebug() << "The slow operation took" << timer.nsecsElapsed() << "nanoseconds";

Documentation of QElapsed Timer