inline function

I have a doubt in inline function vs normal function

why inline function is faster than normal function?

please explain
Last edited on
Use google.
See 'Do inline functions improve performance?' https://isocpp.org/wiki/faq/inline-functions
If you have nothing good to say, say nothing.
OP wrote:

why inline function is faster than normal function?

Who said that they were? That statement is too general to evaluate to either true or false and therefore invalid.

Unless you're willing to crack open your executable with a debugger to see and evaluate yourself what the compiler is doing, which is a perfectly valid thing to do, you're not going to get much use of of inline. Your compiler comes with what is called an optimizer stage which will catch the supermajority of inefficiencies caused by code layout. Give this a look over:
http://www.tantalon.com/pete/cppopt/general.htm#BadOptimizationStrategies
Consider the worst case where a non-inline function is in another compilation unit (C++ file). Let's call it func(). So when the compiler is compiling your program, all it knows is the prototype of the function. When call that function your program has to:
- allocates space for the arguments
- copies the actual arguments to this space (for reference params it will probably be a pointer).
- Saves the return address
- transfers control to func()

Next, func() does it's thing. Then

- func() reads the return address and transfers control back to your program
- Your program deallocates the space for the arguments.
- Since the compiler doesn't know anything about what func() did, the optimizer must be pessimistic and assume that all memory and allowable registers have changed. That makes it hard to optimize anything across the boundary of that function call.

Now consider what happens when the code is inlined. The compile knows exactly what the code will do, so if it chooses to inline the code then your program:

- does func()'s thing.

and that's it.

This is a huge simplification but you get the idea. The bottom line is that function calls are expensive, although many RISC processors have greatly reduced the cost. So inlining functions can improve performance.

Caution: as with all optimization, don't dive into it. Optimize only after the program is working correctly and only when you have hard evidence that the optimization will help.
Topic archived. No new replies allowed.