[Reprinted] use of C language inline function, reprinted inline Function
Use of C language inline functions
Reprinted from: http://blog.chinaunix.net/uid-21843265-id-3056446.html
After learning inline functions in textbooks and defining them as inline functions, the University will save the overhead of function calls and directly nest assembly code to replace function calls and improve efficiency. Inline is rarely used to define functions in projects after work. I have been studying google's c ++ style guide in recent days and found that my previous understanding of inline functions is too superficial, here is a summary.
1. the inline function should not exceed 10 lines of code and cannot contain loops, switches, and if statements.
2. the inline function defined in a c file cannot be directly used in other c files. google recommends defining the inline function in the **-inl. h header file.
3. Do not over-use the inline function definition, especially for large functions.
The above three points describe how to use the inline function correctly. I didn't fully understand inline in the past, but the method of using inline is incorrect. But now the compiler is advanced enough, this ensures that incorrect inline definitions can also be correctly compiled and run. There may be a lack of performance.
I did an experiment on F14 (gcc version 4.5.1 20100924 (Red Hat 4.5.1-4) (GCC:
1. When the inline function contains more than 10 rows and contains loops and switch statements, gcc executes the inline syntax and embeds the inline function assembly into the main function.
1 //gcc n.c -O2 -S 2 #include <stdio.h> 3 #include <string.h> 4 5 //inline int inc_inline(volatile int *j); 6 inline int inc_inline(volatile int *j) 7 { 8 for (;*j < 100; (*j)++) 9 {10 *j += 2;11 (*j)++;12 }13 switch (*j)14 {15 case 1:16 (*j)++;17 break;18 case 2:19 (*j)++;20 break;21 default:22 break;23 }24 25 return (*j)++;26 }27 28 29 int main(int argc, char *argv[])30 {31 volatile int i = 0;32 33 inc_inline(&i);34 35 printf("i;%d\n", i);36 37 return 0;38 }
2. When I put the inline function definition in another c file and declare this function in the main function file, the inline function does not take effect, the compilation compiled by gcc uses call for normal function calls.
// Gcc n. c a. c-O2-S
3. When we use the inline function excessively, the program file will become larger and the performance will be reduced. The program file is certainly larger, but why is the performance reduced? Isn't inline designed to improve the performance? Whether the correctness of the method used can be improved will decrease. The current CPU has a cache, and the compact code is stored in chache for a longer time, which leads to a higher chance of cache hit.
If A function A is not defined as inline and called by many other functions, the function A may be stored in cahe for A long time, in this way, the CPU executes code much faster. If function A is defined as an inline function, the code is scattered among the calling functions. In this way, the code needs to be copied to the cache in the memory for every specified miss, and then executed, this causes great jitter.
For a deeper understanding, when the entire function is compiled as an assembly code, context switching of function calls takes most of the time, you can consider defining this function as an inline function.