What is the difference between macro and inline function relative to execution speed?

How does complier handle inline functions on macros to reduce execution time?

+4
source share
8 answers

The compiler is also allowed to not include the function if it is faster, while the compiler cannot embed the macro. In addition, built-in functions are much safer than macros.

+10
source

Built-in functions are very similar to macros, because both of them are expanded at compile time, but macros are expanded by a preprocessor, and built-in functions are analyzed by the compiler.

+1
source

The most important thing to note here is that a macro is a pure text replacement that the preprocessor performs. The compiler (after the preprocessing step) does not know or care about 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; } 

However, for inline functions, the compiler knows when a function call is inline, and it has much more contextual information about using this. It also means, as other people have said, that this is a request. If the compiler believes that the inclusion of a function is more harmful , then it will leave it as a normal function call. Unlike macros, the compiler will not have code repetition information when using a macro.

Thus, if your code can use a built-in function, use this instead of a macro. You help the compiler improve its code by providing it with additional information.

+1
source

In addition, MACROS do not have access to class member variables. It does not understand. Built-in functions are useful here.

+1
source

There should be no difference in speed (assuming a reasonable little code), because they will both be compiled to the place where they were used / called. Its simply better to use functions because macros may have a way of doing unexpected things.

0
source
  Inline Function Macros 
  • Inline function parsed by compiler | 1. Macros are processed by the preprocessor.
  • built-in function can be written inside | 2. Macros cannot be recorded internally. class. |
  • inline performed a strick type check | 3. Macros do not mint strictly.
  • the built-in function is performed by keyword. | 4. where the macros go to #include |, define.
  • built-in function containing argument | 5. Macros containing the expression must be processed once, and then many can be used | processed every time they are used. time. |
0
source

The built-in functions comply with all type safety protocols that are used in normal functions.

Built-in functions are defined using the same syntax as any other function, except that they include the inline keyword in the function declaration.

Expressions passed as arguments to built-in 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 expand before compilation, so you cannot use them for debugging. However, for built-in functions this is not the case.

scope is globle in the case of a macro, a local scope can be applied to an inline function.

0
source

built-in functions are much better than macros. for example, if a built-in function is large enough to slow down the speed of code execution, the compiler does not make it built-in, since there is no such macro check

0
source

Source: https://habr.com/ru/post/1336495/


All Articles