Search code examples
c++windowsqtqtestlib

Force QBENCHMARK to perform several iterations


How can I perform several iterations using QBENCHMARK?

For example I have this code:

void MyUnitTest::speedText()
{
    QBENCHMARK {
        obj->foo();
    }
}

I looked at the documentation, and it says:

The code inside the QBENCHMARK macro will be measured, and possibly also repeated several times in order to get an accurate measurement. This depends on the selected measurement back-end.

I tried with the default and -tickcounter, but both show only one iteration:

Default (no args):

RESULT : MyUnitTest::speedText():
     89 msecs per iteration (total: 89, iterations: 1)

With -tickcounter:

RESULT : MyUnitTest::speedText():
     210,246,493 CPU ticks per iteration (total: 210,246,493, iterations: 1)

The other 2 don't work - -callgrind is Linux-only while -eventcounter seems to cause the unit test to terminate.

So is there a way to make QBENCHMARK do more than one iteration?


Solution

  • QBENCHMARK runs only once so long as the elapsed time for the iteration exceeds some fixed threshold (the threshold depends on the chosen backend).

    In order to force QBENCHMARK to perform several iterations you will need to set the command line option -iterations 10.