开发者

What is the difference between macro & inline function with respect to execution speed?

How complier treats in开发者_运维问答line functions over macros to reduce execution time?


The compiler is also allowed to not inline the function if doing so would be faster, whereas the compiler cannot not inline a macro. In addition, inline functions are miles safer than macros.


Inline functions are very similar to macros because they both are expanded at compile time, but the macros are expanded by the preprocessor, while inline functions are parsed by the compiler.


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.


And also, MACROS have no access to the class member variables. It doesn't understand scoping. Here, inline functions come useful.


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.


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.


         Inline Function                                        Macros
  1. Inline function are parsed by the compiler | 1. macros are processed by preprocessor.
  2. inline function can be written inside | 2. macros cannot be written inside. the class. |
  3. inline performed strick type checking | 3. macros does not perform strict cheking.
  4. inline function are proceed by the keyword. | 4. where is macros are proceed by #include | ,define.
  5. 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. |


inline functions are far better than macros . for example if inline function is large enough to slow the execution speed of the code, compiler does not makes it inline where as there is no such check on macros

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜