如何测量Qt中的函数运行时间?

Bob*_*bur 1 c++ qt clock time-measurement qtime

我在Qt中调用了argon2 - 内存密集型散列函数并测量其运行时间:

...
QTime start = QTime::currentTime();
// call hashing function
QTime finish = QTime::currentTime();
time = start.msecsTo(finish) / 1000.0;
...
Run Code Online (Sandbox Code Playgroud)

在argon2库的测试用例中,时间以另一种方式测量:

...
clock_t start = clock();
// call hashing function
clock_t finish = clock();
time = ((double)finish - start) / CLOCKS_PER_SEC;
...
Run Code Online (Sandbox Code Playgroud)

我正在调用它们在测试用例中调用的函数.但我的数字增加了两倍(两倍慢).为什么?如何测量Qt中的函数运行时间?clock()实际测量的是什么?

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

Tom*_*ijn 13

您也可以尝试使用QElapsedTimer:

QElapsedTimer timer;
timer.start();

slowOperation1();

qDebug() << "The slow operation took" << timer.elapsed() << "milliseconds";
qDebug() << "The slow operation took" << timer.nsecsElapsed() << "nanoseconds";
Run Code Online (Sandbox Code Playgroud)

QElapsed Timer的文档