ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
>>>>>>>>>>>> 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
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
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?
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.
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.