c++qtclocktime-measurementqtime

How to measure function running time in Qt?


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.


Solution

  • 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