B. D
B. D

Reputation: 7823

QtCreator with MinGW : How to make compiler optimization

I am using QtCreator on windows, and I would like to know how to make my compiler optimize the output.

My understanding is that MinGW is a port of GCC. So, I should be able to use arguments such as -O2. However, in the "Projects" bar, the only things I see are :

So, I tried to add -O2 in the "Make arguments" box, but unfortunately, I get an error "invalid option --O"


For anyone interested, I was trying to make an implementation of the Ackermann function because I read that :

The Ackermann function, due to its definition in terms of extremely deep recursion, can be used as a benchmark of a compiler's ability to optimize recursion

The code (which doesn't really use Qt) :

#include <QtCore/QCoreApplication>
#include <QDebug>
#include <ctime>

using namespace std;

int nbRecursion;
int nbRecursions9;

int Ackermann(int m, int n){
    nbRecursion++;
    if(nbRecursion % 1000000 == 0){
        qDebug() << nbRecursions9 << nbRecursion;

    }
    if(nbRecursion == 1000000000){
        nbRecursion = 0;
        nbRecursions9++;
    }

    if(m==0){
        return n+1;
    }
    if(m>0 && n>0){
        return Ackermann(m-1,Ackermann(m, n-1));
    }
    if(m>0 && n==0){
        return Ackermann(m-1,1);
    }
    qDebug() << "Bug at " << m << ", " << n;

}


int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    nbRecursion = 0;
    nbRecursions9 = 0;

    int m = 3;
    int n = 13;

    clock_t begin = clock();
    Ackermann(m,n);
    clock_t end = clock();
    double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;

    qDebug() << "There are " << CLOCKS_PER_SEC << " CLOCKS_PER_SEC";
    qDebug() << "There were " << nbRecursions9 << nbRecursion << " recursions in " << elapsed_secs << " seconds";

    double timeX = 1000000000.0*((elapsed_secs)/(double)nbRecursion);
    if(nbRecursions9>0){
        timeX += elapsed_secs/(double)nbRecursions9;
    }

    qDebug() << "Time for a function call : " << timeX   << " nanoseconds";

    return a.exec();
}

Upvotes: 0

Views: 2335

Answers (1)

Nikos C.
Nikos C.

Reputation: 51832

-O2 is used by default when you do a release build. Only debug builds don't use optimization. Regardless, if you want to use specific compiler options, you do so in the project file (*.pro) itself, by appending your options to the QMAKE_CFLAGS_RELEASE (for C files) and QMAKE_CXXFLAGS_RELEASE (for C++ files). For example:

QMAKE_CFLAGS_RELEASE += -O3 -march=i686 -mtune=generic -fomit-frame-pointer
QMAKE_CXXFLAGS_RELEASE += -O3 -march=i686 -mtune=generic -fomit-frame-pointer

If you really want to use some specific options always, regardless of whether it's a debug or release build, then append to QMAKE_CFLAGS and QMAKE_CXXFLAGS instead. But usually, you'll only want optimization options in your release builds, not the debug ones.

Upvotes: 2

Related Questions