How to measure function running time in Qt?

纵然是瞬间 提交于 2019-12-04 19:26:28

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

e.jahandar

clock() isn't accurate for measuring time spend in functions. It just returns number of ticks for whole program while its on CPU rightnow, it doesn't count blocking IO operations or sleeps. It just counts ticks which your program is running on CPU (processing). If you put sleep in your code you will loose CPU and this time isn't counting with clock(). You have to use time() or gettimeofday() or more accurate rdtsc assembly instruction.

Lookat these questions :

clock() accuracy

Why is CLOCKS_PER_SEC not the actual number of clocks per second?

In Qt sources, you will see the Qt has used gettimeofday for implementing QTime::currentTime() under Unix https://github.com/radekp/qt/blob/master/src/corelib/tools/qdatetime.cpp : line 1854

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!