Commonplace 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 just use like:
CHECKTIME( // any code for (int i=0; i<1000; i++) { timeConsumingFunc(); } )
exit:
onSpeedChanged: 102 Elapsed time: 2 ms.
Damien Jul 29 '16 at 5:46 2016-07-29 05:46
source share