473,394 Members | 1,761 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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 2875
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

30
by: Will Pittenger | last post by:
Does C# inline functions? I do not see a inline keyword. Is there an implicit inline? Can the compiler select functions for auto-inlining? I am more used to C++ where all these things are...
33
by: Robert Seacord | last post by:
When writing C99 code is a reasonable recommendation to use inline functions instead of macros? What sort of things is it still reasonable to do using macros? For example, is it reasonable to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.