468,170 Members | 2,052 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,170 developers. It's quick & easy.

inline functions -- advantages?

hi all,
i did a small experiment to grasp the advantages of declaring a
function as inline.

inline int fun1();
int main(){
unsigned int start=0,end=0;
asm("rdtsc\n\t"
"mov %%eax, %0\n\t":"=m"(start):);

// cout<<"Start value is "<<start<<endl;
fun1();
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*/
}

i ran above program with inline declaration of fun1 and without inline
declaration of fun1, both times, total time taken was same...
am i doing something wrong?

regards
chinmay
-----------------------------------------------blissful
ignorance--------------------------------------------------------------
~

Jun 9 '06 #1
9 2562
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.
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?

// 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.

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.
}


--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!
Jun 9 '06 #2
chinu <ch**********@gmail.com> wrote:
hi all,
i did a small experiment to grasp the advantages of declaring a
function as inline.

inline int fun1();
int main(){
unsigned int start=0,end=0;
asm("rdtsc\n\t"
"mov %%eax, %0\n\t":"=m"(start):);

// cout<<"Start value is "<<start<<endl;
fun1();
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*/
}

i ran above program with inline declaration of fun1 and without inline
declaration of fun1, both times, total time taken was same...
am i doing something wrong?


Usually you declare a function inline to avoid the One Definition Rule
(ODR) that basically says that a definition can only appear once. If
you declare a function inline, then there can be multiple definitions,
and as long as they are all the same then everything is fine. If some
of the definitions are different, then I believe the behavior of the
resulting program is undefined.

If you are trying to use the inline keyword to get the compiler to
directly inline the code, be aware that it is only a hint to the
compiler, and the compiler is completely free to ignore the hint.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Jun 9 '06 #3
chinu wrote:
hi all,
i did a small experiment to grasp the advantages of declaring a
function as inline.

inline int fun1();
int main(){
unsigned int start=0,end=0;
asm("rdtsc\n\t"
"mov %%eax, %0\n\t":"=m"(start):);
// cout<<"Start value is "<<start<<endl;
fun1();
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*/
}

i ran above program with inline declaration of fun1 and without inline
declaration of fun1, both times, total time taken was same...
am i doing something wrong?


The only guaranteed effect of the inline specifier is that a program doesn't
need to obey the one-definition-rule for a function declared as inline.
The compiler is not required to inline the function, or to not inline a
function that wasn't declared inline. It may take the specifier as a hint
to inline the function or just ignore it. Often, it also depends on the
optimization settings as well as the function's content.

Jun 9 '06 #4
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.
}


Jun 9 '06 #5
chinu wrote:
hi all,
i did a small experiment to grasp the advantages of declaring a
function as inline.

inline int fun1();
This is pointless. A function shouldn't be declared 'inline' and
not be defined. That defeats the purpose of having it 'inline'. Do
you know what 'inline' does?
int main(){
unsigned int start=0,end=0;
asm("rdtsc\n\t"
"mov %%eax, %0\n\t":"=m"(start):);

// cout<<"Start value is "<<start<<endl;
fun1();
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*/
}

i ran above program with inline declaration of fun1 and without inline
declaration of fun1, both times, total time taken was same...
am i doing something wrong?


Yes, you probably don't understand what the point of 'inline' is. When
the compiler is generating code and sees a call to that function, if it
has the body of the function and it's declared 'inline' (a possible hint
to the compiler, saying, "it's OK, compiler, go ahead and stick the body
of that function in there instead of making a true call"), then it can
generate code differently than it would without 'inline'. Compare

inline int onemorethan(int a) {
return a+1;
// code generated:
// function_prologue ; preserving some registers, checking env
// load register from argument
// add 1 to register
// store register to where_return_value_goes ; maybe NOP
// function_epilogue ; restoring preserved registers, blahblah
}

int main() {
int b = 41;
int c = onemorethan(b);
// code generated with 'inline':
// load register from 'b'
// increment register
// store register in 'c'

//// code generated if there were no 'inline'
//// load register from 'b'
//// store register to where_arguments_go ; ususally 'push'
//// call 'onemorethan' ;;; see above
//// load register from where_return_value_comes ; usually NOP
//// store register to 'c'
}

If by the time the compiler encounters a call to 'onemorethan' and
does not know that the body of the function is (and it would be the
case if I just provided a declaration (prototype) and not the body),
the compiler cannot replace the call with the body.

As to ODR violations, the compilers/linkers are not yet sophisticated
enough to really catch those things. They pretend to, but you should
not rely on them to do it right. We can discuss that if you want, let
us know.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 9 '06 #6
In article <11**********************@y43g2000cwc.googlegroups .com>,
"chinu" <ch**********@gmail.com> wrote:
hi all,
i did a small experiment to grasp the advantages of declaring a
function as inline.

inline int fun1();
int main(){
unsigned int start=0,end=0;
asm("rdtsc\n\t"
"mov %%eax, %0\n\t":"=m"(start):);

// cout<<"Start value is "<<start<<endl;
fun1();
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*/
}

i ran above program with inline declaration of fun1 and without inline
declaration of fun1, both times, total time taken was same...
am i doing something wrong?


No. The compiler probably inlined it in both cases (or didn't as the
case may be.)

Stop worrying about such micro-management of the compiler and get the
job done.
Jun 9 '06 #7
Victor Bazarov wrote:
chinu wrote:
hi all,
i did a small experiment to grasp the advantages of declaring a
function as inline.

inline int fun1();


This is pointless. A function shouldn't be declared 'inline' and
not be defined. That defeats the purpose of having it 'inline'. Do
you know what 'inline' does?
int main(){
unsigned int start=0,end=0;
asm("rdtsc\n\t"
"mov %%eax, %0\n\t":"=m"(start):);

// cout<<"Start value is "<<start<<endl;


fun1();
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*/
}

i ran above program with inline declaration of fun1 and without inline
declaration of fun1, both times, total time taken was same...
am i doing something wrong?


Yes, you probably don't understand what the point of 'inline' is. When
the compiler is generating code and sees a call to that function, if it
has the body of the function and it's declared 'inline' (a possible hint
to the compiler, saying, "it's OK, compiler, go ahead and stick the body
of that function in there instead of making a true call"), then it can
generate code differently than it would without 'inline'. Compare

inline int onemorethan(int a) {
return a+1;
// code generated:
// function_prologue ; preserving some registers, checking env
// load register from argument
// add 1 to register
// store register to where_return_value_goes ; maybe NOP
// function_epilogue ; restoring preserved registers, blahblah
}

int main() {
int b = 41;
int c = onemorethan(b);
// code generated with 'inline':
// load register from 'b'
// increment register
// store register in 'c'

//// code generated if there were no 'inline'
//// load register from 'b'
//// store register to where_arguments_go ; ususally 'push'
//// call 'onemorethan' ;;; see above
//// load register from where_return_value_comes ; usually NOP
//// store register to 'c'
}

If by the time the compiler encounters a call to 'onemorethan' and
does not know that the body of the function is (and it would be the
case if I just provided a declaration (prototype) and not the body),
the compiler cannot replace the call with the body.

As to ODR violations, the compilers/linkers are not yet sophisticated
enough to really catch those things. They pretend to, but you should
not rely on them to do it right. We can discuss that if you want, let
us know.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask

hi,
thanks for your reply. But in my gcc-3.4 compiler, your code is not
producing the assembly code, as you explained. i.e. it is not
substituting function call with inc. instruction. i did gcc -S to see
the assembly code.

regards
chinmay

Jun 9 '06 #8
chinu wrote:
[..]
thanks for your reply. But in my gcc-3.4 compiler, your code is not
producing the assembly code, as you explained. i.e. it is not
substituting function call with inc. instruction. i did gcc -S to see
the assembly code.


The example I gave is theoretical ans speculative. 'inline' in real
life is a *hint* and not a *directive*. Compiler is allowed to ignore
it if it so wishes.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 9 '06 #9
chinu wrote:
thanks for your reply. But in my gcc-3.4 compiler, your code is not
producing the assembly code, as you explained. i.e. it is not
substituting function call with inc. instruction. i did gcc -S to see
the assembly code.


Turn on optimizations?

Attempt to inline a simpler function?

--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!
Jun 9 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.

By using this site, you agree to our Privacy Policy and Terms of Use.