Characteristics of inline functions

1. The role of inline (inline function)

We can create an inline function by using the keyword inline together with the function definition. Its function is to reduce the overhead of function calls. If there is a function in our program that will be called frequently, this will make the program run very slowly. Then we can use inline functions to solve this problem and improve the running efficiency of the program.

Definition of inline function:

inline void add(int a, int b){
    return a+b;
}

The above is an inline function. If we frequently use the add operation in our program, declaring it as an inline function can reduce the calling overhead and improve efficiency.

Note:  1. The inline keyword is a "keyword for implementation", not a "keyword for declaration". In other words, inline can only be placed before the definition of a function to make the function internal An associative function is invalid when placed before the function declaration.

         2. Another point is: the member function defined in the C++ class is an inline function by default, but the premise is that it is defined in the class, because our normal way of defining a class is to declare the member variables and member functions of the class in the header file. , Define the member function of the class in the original file. At this time, the definition of the member function occurs outside the class, so it is no longer an inline function. If we want to make it an inline function, we need to manually add inline .

         3. As for the first point, why do I say that the function may be made an inline function, because inline is a recommended keyword, it does not guarantee that the function will become an inline function, but the compiler is compiling When you obtain a function with inline suggestions, judge its scale, content, method and other elements. If it meets the requirements of the compiler, the function will become an inline function, otherwise it will not become an inline function, so whether it can be an inline function or not is completely compiled by the compiler. The device has the final say.

2. Implementation of inline (note that inline functions will not be expanded in debug mode)

Since the inline function can reduce the calling overhead of the function, how does it implement it? Very simple, we expand its code in all the places where the inline function is called, which will increase the amount of code, but reduce it. The overhead of calling is a typical space-for-time practice.

We transfer the compiler to Release mode to use the inline function, and we can see how it is implemented under the assembly instructions.

Source code:

#include<iostream>
using namespace std;

inline int add(int a, int b) {
	return 0;
}

int main() {
	int b=add(1, 2);
	cout << b << endl;
	return 0;
}

(debug mode) disassembly:

In debug mode, we can clearly see that when the program runs to the add function, two parameters 2 and 1 are pushed to the stack, and the add function is called.

(Release mode) Disassembly:

At this point, we can see that, like the previous parameter push stack, there is no function call, that is, the inline function, the implementation is successful, here we will not continue to call the add function, but expand the add function directly, Put its code here to directly perform a+b operation, avoid function calls and increase its operating efficiency.

3. Precautions for using inline

As I mentioned above, using inline functions is a way to consume space and obtain time, so we have to grasp the degree of it. When the scale of the program is too large, the running cost of its code has far exceeded our function call. At this time, the benefit is very small, and it is unnecessary to use inline functions, and inline functions cannot be used when recursion or looping occurs in the function memory, and the compiler will not turn them into inline functions.

Source code:

#include<iostream>
using namespace std;

inline int mul(int a) {//求a的阶乘
	if (a <= 0) {
		return 1;
	}
	return a * mul(a - 1);
}

int main() {
	int b=mul(10);
	cout << b << endl;
	return 0;
}

(Release mode) Disassembly:

At this time, in the disassembly instruction of Release mode, we see that the mul function is not expanded, but a function call is made, indicating that the compiler has not turned it into an inline function.

4. The difference between inline and preprocessing

We know that macro substitution will occur in the preprocessing stage. Macro substitution is to replace the macro-defined code or data with macros where macros are used in the code. Inline is also to copy the code in the function to the calling inline function. place, then what is the difference between them? Macro replacement is just a simple replacement and is not safe, and when an inline function is used, the compiler will judge the correctness of the inline function call. If it is not correct, the operation will not be will happen, so it is safer, so all macro code in C++ should be replaced with inline functions. 

Guess you like

Origin blog.csdn.net/weixin_49312527/article/details/123503198