Any decent compiler in C today tries to force the functions to be inline whenever it is worthwhile to do this, regardless of the code given. Of course it is possible to control this via switch compilation.
The optimization of inline puts the body of the function at the location of its call avoiding having to prepare the environment for the new function - usually save registers and copy arguments - and restore at its completion (prologue and epilogue of function). This makes the code faster under certain circumstances. Optimization can not always reduce code so much, especially it can be difficult to avoid data copies.
It only pays to do it in relatively small jobs and they will perform very quickly. These functions can have a good part of the execution time formalizing the function and not processing what is desired. Functions with multiple instructions and mainly with loop does not usually compensate.
The compiler is usually very smart about this, it’s rare for the programmer to know when to do it inline better than the compiler knows.
There are cases that this optimization enables other possible optimizations, as well as other optimization makes the code suitable for inline.
If the programmer forces where he should not, not only can he lose performance, mainly by filling the cache more easily, but he may also have some problems not always easy to notice.
If the programmer knows how to measure (profile) the application and determine that there really will be gain where the compiler can not perceive, after all this is not easy task, then it may be interesting to force manually.
At some point it was included in the possible syntax to determine whether the code should do it, but compilers don’t usually care much about it, some completely ignore it and decide on their own, others give more weight when the syntax is used, but it doesn’t guarantee anything. Of course a compiler can do what the programmer asked. Some even have another syntax and/or switch that force the use of this optimization.
In your example would probably optimize like this:
int main(void) {
printf("\nStack OverFlow\nFunção inline.\n\n");
printf("\nStack OverFlow\nFunção comum.\n\n");
}
Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.
In the past people used macros to do this. Today it is completely unnecessary in modern compilers. It is much easier to make mistakes with macros. But it is possible to make them also when trying to force the inline. In C there is still the culture of macros use. In C++, no.
The inline
along with static
or extern
may be more useful and give a relevant instruction to the compiler.
Possible duplicate of Iterated inline functions
– Guilherme Nascimento
@Guilhermenascimento I will analyze, if it is duplicate I of the one vote here
– gato