(Inline) Use of inline functions in IOS development, inlineios
Today, when reading the YYKit source code (https://github.com/ibireme/YYKit.git), I found a lot of inline functions in the YYKitMacro. h component, such as a function in this file.
static inline void dispatch_async_on_main_queue(void (^block)()) { if (pthread_main_np()) { block(); } else { dispatch_async(dispatch_get_main_queue(), block); }}
Use this function
dispatch_async_on_main_queue(^{ });
For example, we often use it more concisely:
dispatch_async(dispatch_get_main_queue(), ^{ });
So what are the advantages of using inline functions again? First, let's talk about inline functions. The textbook defines that inline functions are functions modified with the inline keyword. Inline functions do not control the transfer when called, but embed the function body in each call during compilation. During compilation, a macro-like replacement is used to replace the name of the called function with the function body. The C language originally does not support inline, but the inline support provided by C ++ allows many C compilers to implement some extensions that support inline semantics for the C language. C99 puts inline into the Standard C language and provides the inline keyword. Like the inline in C ++, the inline of C99 is also a prompt to the compiler, prompting the compiler to compile according to the definition of inline functions as much as possible to remove the overhead of function calls.
The reason why inline keyword is introduced in C:
The inline keyword is used to define an inline function of a class. The main reason for introducing it is to use it to replace the macro definition in expression form in C.
An example of macro definition in expression form:
# Define ExpressionName (Var1, Var2) (Var1 + Var2) * (Var1-Var2)
Why should we replace this form? Let me say:
1.First, let's talk about the reason why macro definition is used in C. C language is a very efficient language. This macro definition is like a function in form and usage, but it is implemented using a Preprocessor, without a series of operations such as parameter pressure stack and code generation, the efficiency is very high, which is one of the main reasons why it is used in C.
2.This macro definition is similar to a function in form, but when using it, it only performs simple replacement in the pre-processor symbol table, so it cannot detect the parameter validity, therefore, the C compiler cannot strictly check the type, and its return value cannot be forcibly converted to a suitable type for conversion, its use has a series of risks and limitations.
3.The Class and Class access control is introduced in C. In this way, if an operation or expression involves protection members or private members of the class, you cannot use this macro definition.
4.The purpose of inline is to replace the macro definition of this expression form. It eliminates its shortcomings and inherits its advantages.
Note the following before using the inline function:
1. You can use the inline function to completely replace the macro definition in expression form.
2. inline functions are generally used only when the function content is very simple. This is because the code of the inline function is expanded wherever it is called. If the function is too complex, the consequence of code expansion is likely to be greater than the benefits of efficiency improvement.
3. Loop statements and switch statements are not allowed in inline functions. If inline functions have these statements, compilation considers the function as a normal function to generate function call code. recursive functions (self-called functions) cannot be used for inline functions. Inline functions are only applicable to 1 ~ Five rows of small functions. For a large function that contains many statements, the overhead of function calling and return is relatively insignificant, so it is not necessary to use inline functions.
Since the efficiency of using inline functions is improved, I simply thank you for the code verification.
The test code is as follows:
static inline void add(int x, int y);int main(int argc, const char * argv[]) { clock_t start, finish; double duration; start = clock(); for (int i =0,j =0; i<100000; i++,j++) { add(i,j); } finish = clock(); duration = (double)(finish - start) / CLOCKS_PER_SEC; printf( "%f seconds\n", duration ); return 0;}void add(int x, int y){ int k = x+y; printf("%d\n",k); }
The result of running three times in a row is:
0.086523 seconds
0.086504 seconds
0.085425 seconds
Then change the inline function to a normal function and run it three more times.
void add(int x, int y);int main(int argc, const char * argv[]) { clock_t start, finish; double duration; start = clock(); for (int i =0,j =0; i<100000; i++,j++) { add(i,j); } finish = clock(); duration = (double)(finish - start) / CLOCKS_PER_SEC; printf( "%f seconds\n", duration ); return 0;}void add(int x, int y){ int k = x+y; printf("%d\n",k); }
Print result:
0.085639 seconds
0.086934 seconds
0.085713 seconds
From the results, we can see that using inline functions in this test code can indeed improve the running speed. Therefore, we can use inline functions in the project to improve APP performance in the future.