Advertisement

inline functions?

Started by January 21, 2004 05:45 PM
5 comments, last by Tree Penguin 21 years, 1 month ago
What exactly are inline functions, what''s the difference between normal functions and inline functions and why should i use them? Thanks in advance !
"inline" means that the body of that function is just inserted into whereever that function is called.

It gives you slightly faster performance at the cost of increased code size.

You should use them for functions that are extremely simple (like getters and setters)

Note that in C++, when you define a function inside the class definition, this is equivalent to using the "inline" keyword.

Note also that the "inline" keyword is only really a suggestion to the compiler, and it may choose not to inline the function if it doesn't want to.

[edited by - pinacolada on January 21, 2004 6:53:25 PM]
Advertisement
The code is compiled inline with the rest of the section instead of jumping to a seperate point.

Let''s pretend you have a function called func1() and you call it inside a loop. Every iteration of that loop, your code copies the variables and jumps to a seperate section. This jumping and copying takes time, which slows down the function.

Now if the function is only a couple lines long, then it would be faster just to copy all of those lines inline where it''s called, saving a jump and optimally the copying.

The bad thing is, to speed up programs, some compilers "unroll" loops. Instead of unrolling a loop that produces 1000 jumps (let''s say) to a different section, it unrolls the 2 lines times 1000 ( = 2000). This is called code bloat.
Thanks a lot!
use it only when your function is accessed frequently! (basic mathematical algorithms like dot product, etc)

"Knowledge is no more expensive than ignorance, and at least as satisfying." -Barrin
"Knowledge is no more expensive than ignorance, and at least as satisfying." -Barrin
quote:
Original post by NatasDM
The code is compiled inline with the rest of the section instead of jumping to a seperate point.

Let''s pretend you have a function called func1() and you call it inside a loop. Every iteration of that loop, your code copies the variables and jumps to a seperate section. This jumping and copying takes time, which slows down the function.

Now if the function is only a couple lines long, then it would be faster just to copy all of those lines inline where it''s called, saving a jump and optimally the copying.

The bad thing is, to speed up programs, some compilers "unroll" loops. Instead of unrolling a loop that produces 1000 jumps (let''s say) to a different section, it unrolls the 2 lines times 1000 ( = 2000). This is called code bloat.


need to correct it i think. first you can never know what the compiler''s optimizer does. and the number of calls doesnt change. they are simply not organized into a loop. btw a goog optimizer optimizes these frequent calls (tail-call optimizations, etc)
"Knowledge is no more expensive than ignorance, and at least as satisfying." -Barrin
Advertisement
oh and tree penguin! (sorry for the triple post, im really tired)
Never bother about optimizing c++ code (or other code) such ways. lay it on the hands of the optimizers, its more important to write clear code.
btw optimizers often make functions inline automatically if needed (but you can even gain a little performance with well chosen inline functions)
"Knowledge is no more expensive than ignorance, and at least as satisfying." -Barrin

This topic is closed to new replies.

Advertisement