The inline function is function that substitutes at its calling-statement during execution.
- Generally, the compiler skips the execution from the function-call-statement to actual function (function-block).
- But c++ compiler brings actual-function to function-call-statement.
- The inline functions reduce the execution time
- The keyword ‘inline’ used to create an inline function.
Example:
#include<iostream.h>
#include<conio.h>
inline void disp(int n)
{
for (int i=1; i<=n; i++)
cout << i << endl;
}
void main()
{
clrscr();
disp(4);
getch( );
}
Inline functions provide following advantages:
1) Function call overhead doesn’t occur.
2) It also saves the overhead of push/pop variables on the stack when function
is called.
3) It also saves overhead of a return call from a function.
4) When you inline a function, you may enable compiler to perform context
specific optimization on the body of function. Such optimizations are not
possible for normal function calls. Other optimizations can be obtained by
considering the flows of calling context and the called context.
5) Inline function may be useful (if it is small) for embedded systems because
inline can yield less code than the function call preamble and return.
Inline function disadvantages:
1) The added variables from the inlined function consumes additional registers,
After in-lining function if variables number which are going to use register
increases than they may create overhead on register variable resource
utilization. This means that when inline function body is substituted at the
point of function call, total number of variables used by the function also
gets inserted. So the number of register going to be used for the variables
will also get increased. So if after function inlining variable numbers
increase drastically then it would surely cause an overhead on register
utilization.
2) If you use too many
inline functions then the size of the binary executable file will be large,
because of the duplication of same code.
3) Too much inlining
can also reduce your instruction cache hit rate, thus reducing the speed of
instruction fetch from that of cache memory to that of primary memory.
4) Inline function may
increase compile time overhead if someone changes the code inside the inline
function then all the calling location has to be recompiled because compiler
would require to replace all the code once again to reflect the changes,
otherwise it will continue with old functionality.
5) Inline functions may
not be useful for many embedded systems. Because in embedded systems code size
is more important than speed.
6) Inline functions
might cause thrashing because inlining might increase size of the binary
executable file. Thrashing in memory causes performance of computer to degrade.
A Compiler may not
perform inlining in circumstances like:
1) If a function
contains a loop. (for, while, do-while)
2) If a function contains static variables.
3) If a function is recursive.
4) If a function return type is other than void, and the return statement
doesn’t exist in function body.
5) If a function contains switch or goto statement.
No comments:
Post a Comment