PDA

View Full Version : What is the easiest way to test how much time does an operatioin take?



MorrisLiang
12th June 2010, 09:51
I want to know how much time does my function take,so that I can compare and choose the better one.
Is there an easy way to do this?I don't want unit test thing,that seems like a bit overkill.

wysota
12th June 2010, 09:55
It depends what reliability of such measurement you require.
The easiest way is:

QTime t;
t.start();
functionCall();
qDebug() << t.elapsed() << "ms";
but it is completely unreliable, especially if your function is short. Much better is to use QBENCHMARK from QtTestLib (I don't think it is an overkill).

MorrisLiang
13th June 2010, 14:13
I tried QBENCHMARK today,but with no luck.


public slots:
void testUpdateContents()
{
QBENCHMARK
{
updateContents();
}
}

In one of my class, there's a slot called testUpdateContents().It's connected to a QPushButton's clicked() SIGNAL.It complied without any error.
But when I clicked that button,I got "Segmentation fault".
The attachment shows where's the line of code causing the problem.

wysota
13th June 2010, 14:18
What does updateContents() do? By the way, QBENCHMARK should be used in tests, not in regular apps.

MorrisLiang
13th June 2010, 15:34
What does updateContents() do? By the way, QBENCHMARK should be used in tests, not in regular apps.

updateContents() is just a normal function which updates an list of QWidgets.
Ok,I read the tutorial again,finally knows how to use it.Thanks~

wysota
13th June 2010, 16:03
updateContents() is just a normal function which updates an list of QWidgets.
If what the function does is that it schedules some events (i.e. by calling update() on widgets) then measuring the time it takes to finish the updateContents() function doesn't yield any practical value as most work is done already after that function returns (in the event loop).