Home > Software design >  QStringList creation in place for parameter passing
QStringList creation in place for parameter passing

Time:07-24

I have a function that gets a QStringList as a parameter. The QStringList is created in place for parameter passing. Two possibilities come to my mind for this:

myFunction(QStringList() << myQString); // possibility 1
myFunction(QStringList { myQString }); // possibility 2

Which possibility is more performant?

CodePudding user response:

Here is the benchmark:

#define ct_Benchmark(EXPR, repeat){\
    int repeated = repeat;\
    qint64 initialTime = QDateTime::currentMSecsSinceEpoch();\
    qint64 totalTime   = initialTime;\
    EXPR;\
    initialTime = QDateTime::currentMSecsSinceEpoch() - initialTime;\
    for ( int ___i_ = 0; ___i_ < repeated; ___i_  ) {\
        EXPR;\
    }\
    totalTime = QDateTime::currentMSecsSinceEpoch() - totalTime;\
    qreal averageTime = qreal( totalTime / repeated );\
    const QString expression(#EXPR);\
    const QString iterations(" Cycles: "   QString::number( repeated    ) );\
    const QString initial   ("Initial: "   QString::number( initialTime ) );\
    const QString total     ("  Total: "   QString::number( totalTime   ) );\
    const QString average   ("Average: "   QString::number( averageTime, 'g', 24 ) );\
    ct_Info(expression, iterations, initial, total, average);\
}
    QString sneed("'s feed & seed. Formerly chucks.");
    ct_Benchmark( QStringList{ sneed }, 1000000 );
    ct_Benchmark( QStringList( QStringList() << sneed ), 1000000 );

Debug

Info /home/anon/Programming/QtConsoleDesigner/QtSandbox/src/sandbox.cpp:57
int main(int, char**)

QString expression
"QStringList( QStringList() << sneed )"

QString iterations
" Cycles: 10000000"

QString initial
"Initial: 0"

QString total
"  Total: 3461"

QString average
"Average: 0"
Info /home/anon/Programming/QtConsoleDesigner/QtSandbox/src/sandbox.cpp:56
int main(int, char**)

QString expression
"QStringList{ sneed }"

QString iterations
" Cycles: 10000000"

QString initial
"Initial: 0"

QString total
"  Total: 2094"

QString average
"Average: 0"

Release:

Info /home/anon/Programming/QtConsoleDesigner/QtSandbox/src/sandbox.cpp:57
int main(int, char**)

QString expression
"QStringList( QStringList() << sneed )"

QString iterations
" Cycles: 10000000"

QString initial
"Initial: 0"

QString total
"  Total: 545"

QString average
"Average: 0"
Info /home/anon/Programming/QtConsoleDesigner/QtSandbox/src/sandbox.cpp:56
int main(int, char**)

QString expression
"QStringList{ sneed }"

QString iterations
" Cycles: 10000000"

QString initial
"Initial: 0"

QString total
"  Total: 379"

QString average
"Average: 0"

Compiled using GCC. Possibility 1 QStringList{ sneed } is about a third faster.

  • Related