0

I want to have a function that, also has an inline version. and i want them to be identical.

would this be efficient, or are there better ways, than to copy by hand?

void inline in_function(int a) {
  printf("%d", a);
}
void function(int a) {
  in_function(a);
}
  • 1
    may I ask why you'd implement the same function both ways? Note that inline is all but vestigial at this point - https://stackoverflow.com/questions/29796264/is-there-still-a-use-for-inline#:~:text=As%20compilers%20became%20better%20at,finds%20that's%20a%20better%20optimisation. – erik258 Sep 19 '20 at 15:23
  • See also: https://stackoverflow.com/questions/1932311/when-to-use-inline-function-and-when-not-to-use-it, https://stackoverflow.com/questions/216510/what-does-extern-inline-do, https://stackoverflow.com/questions/1137575/inline-functions-vs-preprocessor-macros, https://stackoverflow.com/questions/7762731/whats-the-difference-between-static-and-static-inline-function and https://stackoverflow.com/questions/6312597/is-inline-without-static-or-extern-ever-useful-in-c99 – Jonathan Leffler Sep 19 '20 at 15:52
  • Note that if a function is `static` in a source file, the compiler (optimizer) will often inline it anyway (even without the `inline` function specifier) if it considers it advantageous to do so. For example, if the function is only called in one place, it may be inlined. If it is small, it may be inlined even though it is not marked `inline` and is called in many places. It cannot be inlined if you generate a function pointer for it — or, at least, there must be a non-inline version of the function for the pointer to reference. Working between files is trickier — see the x-refs. – Jonathan Leffler Sep 19 '20 at 15:55

1 Answers1

2

First, one needs to understand the inline function vs normal function internals. Those could be

Compilation

Normal Function Compilation

For normal function compiler performs a "JUMP" or "LONG JUMP" instruction to reach to the function definition.

Inline Function

When compiler comes across a inline function, compiler replaces the function call and inserts the entire function body within the code. This process is called expansion

Overhead

Usually, during execution to reach a normal function, the code executor needs to perform a "jump" or "long jump" to the function definition, to do that, the executor needs to push all the current cpu registers in to stack and perform the jump. this creates a additional overhead, whereas for inline function this does not occur.

Memory Usage

Inline functions, since they are repeatedly expanded at several places, wastes memory but provides a performance advantage over normal function. Normal functions calls saves memory but bottleneck is the degraded in performance

When to use inline function

If the time needed to jump to a function call exceeds the time needed to execute the function, then it is not advisable to segment it with a normal function but rather use inline function for such purposes.

Conclusion

void inline in_function(int a) {
    printf("%d", a);
}
void function(int a) {
    in_function(a);
}

Finally, the above way of calling an inline function within a normal function goes against the idea of normal function and inline function usage and principles. since it does not provide a performance or memory advantage.

choppe
  • 493
  • 2
  • 11