How complier treats inline functions over macros to reduce execution time?
- 3This is a premature optimization that you should not care about. Functions are safer for a whole bunch of reasons. Also functions are only inlined if the compiler decides that is a good idea (as usually a human choice to inline is the wrong choice now (or potentially later after the function is maintained)). It is the compiler job to make stuff fast (you just hamper the compiler from doing its job when you try and prematurely optimize stuff).Loki Astari– Loki Astari2011-01-21 17:02:14 +00:00Commented Jan 21, 2011 at 17:02
- Can you please explain what is premature optimization?Chandrakant– Chandrakant2011-01-21 17:09:12 +00:00Commented Jan 21, 2011 at 17:09
- 1Please read this link. en.wikipedia.org/wiki/Inline_function#Comparison_with_macrosuser124493– user1244932011-01-21 17:10:33 +00:00Commented Jan 21, 2011 at 17:10
- 2"premature optimization" You are trying to optimize the code by applying a specialized technique. Rather than just writing the code as clearly and concisely as possible. There is normally no point in thinking about these kind of optimizations until you have completed the program and timed the different parts to find the bottleneck. The fact the compiler already does this kind of optimization is another strike against this type of optimization as a human will never be able to beat the compiler (in the long run).Loki Astari– Loki Astari2011-01-21 17:12:32 +00:00Commented Jan 21, 2011 at 17:12
- @Martin York I wish all of SO would read your comment. The "premature optimization" quote is thrown aronund too often here. There's a difference between good planning for obvious scalability and maintainability problems and worrying about unpredictable things. Your use of the phrase in this instance is spot-on and a good representation of what Knuth meant, I am sure.user124493– user1244932011-01-21 17:32:11 +00:00Commented Jan 21, 2011 at 17:32
8 Answers
The most important thing to note here is that a macro is pure text substitution that is done by the preprocessor. The compiler(after preprocessing step) does not know or care what a macro is.
Example:
//this: #define PRINT(s) std::cout << s; int main(int, char**) { PRINT("hello world") return 0; } //Will have identical codegen(in the same compiler version of course) to this: int main(int, char**) { std::cout << "hello world"; return 0; } For inline functions however, the compiler knows when a call to a function is inlined, and it has much more contextual information about the usage of it. This also means, as other people have mentioned, that it is a request. If the compiler deems that inlining the function is more harmful, then it will leave it as a regular function call. In contrast of macros, the compiler will have no information about code repetition when using a macro.
In summary, if your code can be using an inlined function, then use that instead of a macro. You are helping the compiler make your code better by providing it with more information.
Comments
There should be no speed difference (assuming reasonable small code), because they both will get compiled into the place where they were used/called. Its just better practice to use functions, because macros can have a way of doing unforseen things.
7 Comments
Inline functions follow all the protocols of type safety enforced on normal functions.
Inline functions are specified using the same syntax as any other function except that they include the inline keyword in the function declaration.
Expressions passed as arguments to inline functions are evaluated once. In some cases, expressions passed as arguments to macros can be evaluated more than once.
Another thing to keep in mind is that macros are expanded at pre-compile time, so you cannot use them for debugging. For inline functions, however, this is not the case.
scope is globle in case of macro, local scope can be applied to inline function.
Comments
Inline Function Macros - Inline function are parsed by the compiler | 1. macros are processed by preprocessor.
- inline function can be written inside | 2. macros cannot be written inside. the class. |
- inline performed strick type checking | 3. macros does not perform strict cheking.
- inline function are proceed by the keyword. | 4. where is macros are proceed by #include | ,define.
- inline function containing argument are | 5. macros containing expression have to be processed once and then can be used many | processed every time they are used. time. |