What is the purpose of using inline functions in the C language? [duplicate]

6

I would like to know what is the purpose of inline functions in the C language? Is there any performance difference or other features that should be taken into consideration compared to common functions?

Example of an inline function:

inline void funcao_inline_mensagem()
{
    printf("\nStack OverFlow\nFunção inline.\n\n");
}

Example of a common function:

void funcao_comun_mensagem()
{
    printf("\nStack OverFlow\nFunção comum.\n\n");
}

Complete implementation of the two examples to be reproduced :

#include <stdio.h>

inline void funcao_inline_mensagem()
{
    printf("\nStack OverFlow\nFunção inline.\n\n");
}

void funcao_comun_mensagem()
{
    printf("\nStack OverFlow\nFunção comum.\n\n");
}

int main(void)
{
    funcao_comun_mensagem();

    funcao_inline_mensagem();

    return 0;
}
    
asked by anonymous 30.01.2016 / 23:10

1 answer

7

Any decent C compiler today tries to force functions to be inline whenever it compensates for doing so, regardless of which code is determined. Of course you can control this via switch .

Inline optimization places the function body at the location of your call, avoiding having to prepare the environment for the new function - usually saving registers and copying arguments - and restoring at the end of the call (foreword and epilogue of function). This makes the code faster under certain circumstances. Optimization can not always reduce both the code, especially it can be difficult to avoid copies of data.

It only pays to do relatively small functions that will run very quickly. These functions can take much of the execution time by formalizing the function and not processing what you want. Functions with multiple instructions and mainly with loop do not usually compensate.

The compiler is often very clever about this. It's rare for the programmer to know when to do better inline than the compiler knows.

There are cases where this optimization enables other possible optimizations. As well as other optimization makes the code fit for inline .

If the programmer forces where he should not, not only can he lose performance, especially by filling the cache more easily, but he may also have some problems that are not always easy to understand.

If the programmer knows how to measure the application and determines that there will actually be a gain where the compiler can not understand it, this is not an easy task, so it can be interesting to manually force.

At some point it was included in the possible syntax to determine if the code should do so. But compilers often do not care much about it, some completely ignore it and decide on their own, others give more weight when the syntax is used, but it does not guarantee anything. Of course a compiler can do what the programmer asked for. Some even have another syntax and / or switch that forces the use of this optimization.

In your example it would probably optimize like this:

int main(void) {
    printf("\nStack OverFlow\nFunção inline.\n\n");
    printf("\nStack OverFlow\nFunção comum.\n\n");
    return 0;
}

In the past people used macros to do this. Today it is completely unnecessary in modern compilers. It is much easier to make macros. But you can also commit them when you try to force the inline . In C there is still the culture of using macros. In C ++, no.

The inline along with static or extern can be more useful and give a relevant statement to the compiler.

30.01.2016 / 23:50