I am trying to approximate the function call overhead in C. So I have an empty function with the attribute((optimize("O0"))), so that it is not optimized away by GCC.
int __attribute__((optimize("O0"))) func(int a)
{
return (a+a);
}
I am using the method described in the paper http://www.intel.com/content/www/us/en/embedded/training/ia-32-ia-64-benchmark-code-execution-paper.html to determine the time, so its pretty accurate.
So I call the function in a loop multiple times and measure the time to execute:
for (i = 0; i < 10; i++)
{
t1 = start_timer();
x = func(i);
t2 = end_timer();
time = t2 - t1;
}
I notice that the first time the function is called (i=0), it takes more cycles (~10x) than the subsequent calls. Why does this happen?