/**********************************************************************************************
 * test_qtconcurrent.cpp

 * A good overwiew about qtconcurrent is given in the qt documentation:
 * http://doc.qt.io/archives/qt-5.8/qtconcurrentrun.html
 *
 * The GNU and VC compiler work off the threads differently. Vc in order of call, GNU not!
 * MinGW32 is extremely slow (factor 2 in comparison to VC32, factor 7.5 in single thread in
 * comparison to VC64, factor 4 in multithread performance which is probably the result of a
 * lower clock frequency) on an intel i7 6700HQ
 *
 *    compiler              Debug(single/multithread)    release(single/multithread)
 * ------------------------------------------------------------------------------------
 * MinGW 32-bit                 5891 / 4594 ms              5141 / 4047 ms
 * MS VS2015 32-bit             2891 / 3157 ms              2406 / 2847 ms
 * MS VS2015 64-bit             2407 / 2422 ms               688 /  969 ms
 *
 * AW - 31.12.2017
 * ********************************************************************************************/


#include <QCoreApplication>
#include <QtConcurrent>
#include <QFuture>
#include <QTime>


void myFunction(QString name)
{
    for( int i = 0; i <= 5; ++i )
    {
        quint64 s = 0;
        for( quint64 j = 0; j < (1000*1000*10); ++j )
            s += atan(double(j));               // do something time consuming

        qDebug() << name << " s:" << s;
    }
}

int main( int argc, char *argv[] )
{
    QCoreApplication app(argc, argv);

    QTime t;

    t.start();
    myFunction("A");
    qDebug("Time elapsed: %d ms", t.elapsed());

    t.start();
    QFuture<void> t1 = QtConcurrent::run(myFunction, QString("A"));
    QFuture<void> t2 = QtConcurrent::run(myFunction, QString("B"));
    QFuture<void> t3 = QtConcurrent::run(myFunction, QString("C"));
    QFuture<void> t4 = QtConcurrent::run(myFunction, QString("D"));
    t1.waitForFinished();
    t2.waitForFinished();
    t3.waitForFinished();
    t4.waitForFinished();
    qDebug("Time elapsed: %d ms", t.elapsed());

    return app.exec();
}
