Reputation: 85
I am trying to check the time spent in two functions to compare them.
The functions take two backiterators as inputs.
these two are called inside one function.
void b_mark()
{
for(int i=0;i<10;i++)
{
timer1.start();
function1(std::back_inserter(itr1));
timer1.stop();
timer2.start();
function2(std::back_inserter(itr2));
timer2.stop();
}
}
It turns out that the function I call first takes more time and does not matter if it is function1 or function2.
What can be the general reason for this?
The timer is a wrapper to the std::clock()
.
Upvotes: 1
Views: 71
Reputation: 22241
Whenver you want to measure something, you have to make sure that:
In your case I suspect that:
timer
class may be incorrect and cause the result to be invalidIn short: this test case seems like a not reliable one and its results are likely to be invalid.
Try modifying the code like this:
void b_mark()
{
int count = 10000;
timer1.start();
for(int i=0;i<count;i++)
{
resetItr(itr1);
function1(std::back_inserter(itr1));
}
timer1.stop();
timer2.start();
for(int i=0;i<count;i++)
{
resetItr(itr2);
function2(std::back_inserter(itr2));
}
timer2.stop();
}
Upvotes: 1
Reputation: 5808
The time you're measuring includes the time for std::back_inserter(...)
. Could it be that this takes more time the first time you call it, e.g. for initialization?
Upvotes: 1