Goutham
Goutham

Reputation: 293

Time of execution in Qt Creator for C++

How do I modify the .pro file in a C++ project to print the time of execution of a particular code? I use Qt Creator as a general C++ IDE in Ubuntu 13.04.

In terminal, I would use

time ./a.out

My current .pro file is

TEMPLATE = app
CONFIG += console
CONFIG -= app_bundle
CONFIG -= qt

SOURCES += main.cpp

QMAKE_CXXFLAGS += -std=c++0x

Upvotes: 0

Views: 1303

Answers (2)

Damien
Damien

Reputation: 1552

Expending the 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 simple use as:

CHECKTIME(
    // any code
    for (int i=0; i<1000; i++)
    {
       timeConsumingFunc();
    }
)

In your case, if you want the entire program execution time, you can simply include the whole content of main on that macro. Need to be careful about the event loop i suppose.

output:

onSpeedChanged : 102 Elapsed time: 2 ms.

Upvotes: 0

Wazery
Wazery

Reputation: 15863

Why not trying the QBENCHMARK? As the code below shows, you can insert the code you want to measure inside a QBENCHMARK macro. Reference

class MyFirstBenchmark: public QObject
{
    Q_OBJECT
private slots:
    void myFirstBenchmark()
    {
        QString string1;
        QString string2;
        QBENCHMARK {
            string1.localeAwareCompare(string2);
        }
    }
};

Upvotes: 3

Related Questions