Re: inline functions -- advantages?
Phlip wrote:
chinu wrote:
i did a small experiment to grasp the advantages of declaring a
function as inline.
Sometimes that makes things slower.
inline int fun1();
I thought 'inline' methods had to appear in body above their call sites.
No. Usually, to give the compiler a chance to really inline the function,
the body has to appear in the same translation unit as the call, but that's
it.
Maybe not, but I would move the function itself here.
int main(){
unsigned int start=0,end=0;
asm("rdtsc\n\t"
"mov %%eax, %0\n\t":"=m"(start):);
Why are you using assembly to collect the clock() time?
I guess because he didn't find a C++ function to get the execution time with
a resolution of one clock cycle.
// cout<<"Start value is "<<start<<endl;
fun1();
Why not call fun1 in a loop of 1,000,000 ticks, to amplify its running
time?
asm("rdtsc\n\t"
"mov %%eax, %0\n\t":"=m"(end):);
cout<<"time taken is "<<end-start<<endl;
}
inline int fun1(){
int a=1990;
a++;
for(a=0;a<1000;a++);
/* just to consume some time*/
Did you disassemble your code to see if this truly inlined? (Did you
compile in Debug mode?)
Some optimizers will throw away the for() loop when they detect it adds no
value.
Yes, especially if optimzations are enabled, which may be needed to make the
compiler actually inline the function. But the whole content of the
function and not just the for loop is a candidate for being optimized away,
because the variable a is never used for anything. One way to prevent such
optimizations would be to declare a as volatile.
Try again with all my suggestions. And also try other tweaks, such as
changing a to a float, to see if you can detect their performance times,
too.
}