![]() ![]() Most compilers are smart enough to avoid inline functions if debugging is to be done. In fact, most of the implementations of the standard C libraries have implemented min, max, sort and several other functions using Macro functions (the official term for macro functions is smart macros). Inline double max_double(double a, double b) Have a look at this example where we have a generic function that can take in integers, floats, chars, doubles or even pointers and returns the maximum of the 2 numbers. Macros are not type-safe since its just text substitutions, and this feature/flaw can be used to our advantage! So what is the need for these macro functions when we can use inline functions? Situation#1: Generic functions So is the problem solved? Do we have the best solution/replacement for macro functions? Not quite yet! There are places where regular functions cannot replace macro functions.! Let’s see what those places are Macro specific situations To summarise, if the function logic is small enough, it’s better to make it inline to avoid function switching overhead. Now if we don’t inline that function then it will take a total of 10 assembly instructions to execute, but if we inline it we can do it in just 4 assembly lines.! Thereby reducing the code size and improving performance at the same time. Very small function, which when translated to assembly language will give usĪlso, assume in our system the overhead of a function call is 3 instructions to push onto the stack and 3 instructions to pop it back from the stack so a total of 6 assembly instructions for the overheads associated with function calls. Let’s look at the scenario where the inline function can actually reduce code size. ![]() I guess you already understand the scenario where it increases code size, which is a result of code substitution. ![]() The inline function can sometimes increase the code size and sometimes reduce it. The compiler that we need to inline this function the same way as we can doĪchieved both readability and performance by using inline functions! Inline functions and code size Printf("MEDIAN of x,y,z = %d", median(x, y, z)) Īdd the word “inline” in front of our function declaration to tell ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |