Search code examples
coptimizationtiming

Nonlinear behavior of function run time


I have a function which computes a sum and depending on the result of the sum does a certain task or gives a warning message. I need to run this function millions of times in a particle trajectory simulation (it calculates the time and position dependent force) and noticed that my code was very slow.

This is my MWE:

#include <stdio.h>
#include <math.h>

int foo()
{
    double sum =0;
    double dummy_sum = 0;

    for (size_t i=0; i<40; i++)
    {
        sum+=1e-2; 
        dummy_sum += 1e-2;     
    }

    if (sum>5.) // normally this should not happen
    {
        printf("warning message!\n");
        return(-1);
    }
    else if(sum >0)
    {
       // normal behavior
    }

    return(0);
}

int main()
{
   int fooint;

   for(size_t i=0; i<5e9; i++)
   {
       fooint = foo();
       if(fooint!=0)
       {
           return(fooint);
       }
   }
   return 0;
}

I compile with gcc -Ofast -std=c99 test.c -o test.exe using gcc version 4.8.5.

In order to find a way to optimize my function I ran time ./test.exe which indicated a run time of about 38s on my machine.

When I removed the lines

        printf("warning message!\n");
        return(-1);

or the line

        sum+=1e-2; 

the run time was reduced to about 6s.

Also when I changed the number of iterations in the function loop to 35 the speed is increased and the run time is 6s. Increasing it to 36 gives a run time of 33s.

I compiled and ran the code on a different machine with the same linux and gcc versions and got similar results (run times were slightly longer as the machine is older).

What may cause this strange behavior?


Solution

  • It's a GCC bug. Clang optimises your code properly to

    foo:                                    # @foo
            xor     eax, eax
            ret
    main:                                   # @main
            xor     eax, eax
            ret
    

    at near-zero run time.

    Live demo