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

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-03 17:00:56

问题


How complier treats inline functions over macros to reduce execution time?


回答1:


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.




回答2:


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.




回答3:


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.




回答4:


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




回答5:


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.




回答6:


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.




回答7:


         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. |



回答8:


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



来源:https://stackoverflow.com/questions/4761504/what-is-the-difference-between-macro-inline-function-with-respect-to-execution

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!