cow boy
cow boy

Reputation: 85

Benchmarking Two Functions

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

Answers (2)

Dariusz
Dariusz

Reputation: 22241

Whenver you want to measure something, you have to make sure that:

  • each measurement situation is exactly the same as all others - this includes generating same startup data, closing open resources before opening them again, etc
  • you repeat the measured step enough times, so that the results are a) measurable, b) statistically significant
  • the measurement functions/objects themselves are correct

In your case I suspect that:

  • you measure each function call once, which may make the result meaningless - either because of system clock resolution, system resources allocation - and probably for many more reasons
  • you may be operating on the same colleciton, which may be modified during the first call; the first call may, for example, allocate a large amount of memory in the collection, while the second only uses it
  • your timer class may be incorrect and cause the result to be invalid

In 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

nickie
nickie

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

Related Questions