Strange profiler behavior: same functions, different performances

Posted by arthurprs on Stack Overflow See other posts from Stack Overflow or by arthurprs
Published on 2010-06-12T15:56:06Z Indexed on 2010/06/12 16:03 UTC
Read the original article Hit count: 164

Filed under:
|
|
|

I was learning to use gprof and then i got weird results for this code:

int one(int a, int b)
{
    return a / (b + 1);
}

int two(int a, int b)
{
    return a / (b + 1);
}

int main()
{
    for (int i = 1; i < 30000000; i++)
    {
        two(i, i * 2);
        one(i, i * 2);
    }
    return 0;
}

and this is the profiler output

  %   cumulative   self              self     total           
 time   seconds   seconds    calls  ns/call  ns/call  name    
 48.39      0.90     0.90 29999999    30.00    30.00  one(int, int)
 40.86      1.66     0.76 29999999    25.33    25.33  two(int, int)
 10.75      1.86     0.20                             main

If i call one then two the result is the inverse, two takes more time than one
both are the same functions, but the first calls always take less time then the second

Why is that?

Note: The assembly code is exactly the same and code is being compiled with no optimizations

© Stack Overflow or respective owner

Related posts about c++

Related posts about Performance