LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-13-2004, 06:17 AM   #1
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
C++ inline function: Use or don't use?


Hi all,

I finally have some time to dive, head first, into C++. There's yet a lot I have to learn, but the first question already came up:

One of the topics I'm reading at the moment deals with inline functions. The concept is clear to me and it seems like a clever way to make your code (a bit) faster. But at the end of the chapter there is a note that makes me wonder:

note
Inline is a hint to the compiler that you would like the function to be inlined. The compiler is free to ignore the hint and make e real function call.


Google, being one of my best friends, doesn't clear things up for me.

I found out that there is a hidden compiler option to force inline functions (make it an order, not a hint). I also read that modern compilers are good enough to decide for themself (even if the inline option isn't given).

My question:

Is using inline functions outdated and are (modern = gcc 2.95.x/3.3.X) compilers reallly good enough to go around without using it?
 
Old 09-13-2004, 07:03 AM   #2
masand
LQ Guru
 
Registered: May 2003
Location: INDIA
Distribution: Ubuntu, Solaris,CentOS
Posts: 5,522

Rep: Reputation: 69
hi there

as u have said it all that

>>>>>>>>>>>> I also read that modern compilers are good enough to decide for themself (even if the inline option isn't given),<<<<<<<<<<
so it de[aendds upon th gcc that takes in to consideration the avaliable resources and then accordinglt decide whther to

leave a inline function as inline or whehther to make a normal function as inline(it itself makes small non inline function as inline if that improves the effeciency)

so in my opinion we ahoudl do our part(mention the inline functions as of where required) and leave the rest to compilers

regards
 
Old 09-13-2004, 08:10 AM   #3
gamehack
Member
 
Registered: Jun 2003
Location: Sevenoaks, UK
Distribution: Ubuntu
Posts: 183

Rep: Reputation: 30
So, generally speaking if the function is quite small and clever it's good to be inline But if it's complex and it's not the most elegant and smart way of doing it, don't use inline

Cheers,
gamehack
 
Old 09-13-2004, 08:27 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532

Original Poster
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Thanks for the answers this far.

But........

Besides programming style (masand), which is a good point, I still don't see why you should use inline declarations when using modern compilers. The programmer cannot control if inline is actually used, the compiler does. Even if you don't use inline the compiler can decide to use it anyway and vice versa

To me this looks like something that a programmer should not be concerned about while writing code, due to the inability to actually manipulate it.

So, besides the style issue, is there another good reason to use it?
 
Old 09-13-2004, 08:31 AM   #5
llama_meme
Member
 
Registered: Nov 2001
Location: London, England
Distribution: Gentoo, FreeBSD
Posts: 590

Rep: Reputation: 30
Probably the best thing to do is only to use it as a last resort. If your code's running too slowly, profile it, and if a lot of time is being spent in a few small functions, you might get a little extra speed by making them inline. I expect that modern compilers are good enough to avoid extreme cases of inefficiency due to functions not being inlined (e.g. a small function being called 1000s of times in a loop).

Alex

Last edited by llama_meme; 09-17-2004 at 06:07 PM.
 
Old 09-13-2004, 09:38 AM   #6
dakensta
Member
 
Registered: Jun 2003
Location: SEUK
Distribution: Debian & OS X
Posts: 194

Rep: Reputation: 35
Quote:
The programmer cannot control if inline is actually used, the compiler does.
Yes they can ... in fact, you noted this in your first post!

e.g. there are at least five different switches directly controlling the inlining of functions with gcc and what gets inlined, what doesn't, what can't be and how the compiler determines what is inlined and what isn't are all covered.

linky:
http://gcc.gnu.org/onlinedocs/gcc-3....ne.html#Inline
http://gcc.gnu.org/onlinedocs/gcc-3....mize%20Options

The point is that the language does not determine where inline is used, but the implementation does. That doesn't mean that you don't have any control over it though.

You could argue that the programmer is unlikely to make a better decision than the compiler (as with inline assembly) but there again if you need to improve performance then ...

Quote:
... profile it, and if a lot of time is being spent in a few small functions, you might get a little extra speed by making them inline.
Which is, imho, a lot easier to do with a keyword, than it is to work out why certain functions are not being inlined when you thought they should be according to whatever voodoo the compiler has conjured up.

Don't forget that inline makes the source available in situ which in turn can lead to further performance gains.
 
Old 09-13-2004, 10:30 AM   #7
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532

Original Poster
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
This clears things up a bit for me.

The book I'm using did not mention the compile step being important when using the inline construct. They only mention the inline option when writing code and up 'till now I used g++ name.c++ -oname to compile.

Your reply and the links given make it clear that the compile step is very important. From one of the links: Inlining of functions is an optimization and it really "works" only in optimizing compilation. If you don't use -O, no function is really inline.

And to all repliers: Thanks!
 
Old 09-13-2004, 04:08 PM   #8
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
As an aside

Macros in general tend to cause problems, but you can use them to inline very small functions, with or without the compiler's consent.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
inline function means alaios Programming 9 08-31-2005 07:03 AM
inline function and macros in C++ carthyc Linux - General 2 05-14-2005 12:16 AM
Defining a non-inline function inside class definition (tm) Programming 6 05-11-2005 09:14 AM
C++ inline functions Slickfty2 Programming 12 04-23-2003 07:13 PM
gcc: compilation with extern inline function philipsyyy Linux - Software 0 10-13-2002 11:52 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:16 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration