How can i create an inline copy of an function

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);
}

Upvotes: 0

Views: 116

Answers (1)

choppe
choppe

Reputation: 506

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.

Upvotes: 2

Related Questions