(inline) Use of inline functions in iOS development

Source: Internet
Author: User

Today, when reading the Yykit source code (HTTPS://GITHUB.COM/IBIREME/YYKIT.GIT), I found a lot of inline functions used in the YYKitMacro.h component, such as a function in this file

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

Dispatch_async (Dispatch_get_main_queue (), ^{            });

So what's good about using inline functions again? First say what is an inline function, a textbook definition: an inline function is a function that is decorated with the inline keyword. The inline function does not take control over the call, but instead embeds the body of the function at compile time at each call. At compile time, similar to macro substitution, use the function body to replace the function name at the call. C language originally does not support inline, but C + + native to inline support so many C compiler also for C language implementation of some extensions to support the inline semantics. C99 the inline into standard C language and provides the inline keyword. Like inline in C + +, C99 's inline is also a hint to the compiler, prompting the compiler to compile as much as possible with the definition of an inline function, removing the overhead associated with a function call.

Reasons for introducing the inline keyword in c:
The inline keyword is used to define an inline function of a class, and the main reason for introducing it is to replace the macro definition in the form of an expression in C.


An example of a macro definition in the form of an expression:
#define EXPRESSIONNAME (VAR1,VAR2) (VAR1+VAR2) * (VAR1-VAR2)

Why replace this form, and listen to my word:

1. first of all, the reason for the use of this form of macro definition in C, C is a very efficient language, the macro definition in form and use like a function, but it uses the preprocessor implementation, there is no parameter stack, code generation, and so a series of operations, therefore, the efficiency is very high, This is one of the main reasons why it is used in C.

2. This macro definition is similar in form to a function, but when it is used, it is simply a simple substitution in the preprocessor symbol table, so it cannot detect the validity of a parameter, nor can it enjoy the benefit of strict type checking by the C compiler, and its return value cannot be cast to the appropriate type of convertible. In this way, its use has a series of hidden dangers and limitations.

3. access control for classes and classes is introduced in C, so that if an operation or an expression involves a protected member or a private member of a class, you cannot use the macro definition to implement it.

4. The purpose of the inline rollout is to replace the macro definition in this form of expression, which eliminates its drawbacks and, at the same time, inherits its merits well.

When using the inline function, be aware that
1. You can use the inline function to completely replace the macro definition in the form of an expression.

2, inline functions are generally only used when the function content is very simple, this is because the code of the inline function will be in any place to call it, if the function is too complex, the consequences of code bloat is likely to outweigh the benefits of efficiency gains.

3. Loop statements and switch statements are not allowed within inline functions. If the inline function has these statements, then the compiler generates the function call code as if it were a normal function, and the recursive function (which calls its own function) cannot be used to do inline functions. Inline functions are only suitable for small functions that only have a row of the line. For a large function that contains many statements, the overhead of function calls and returns is relatively insignificant, so it is not necessary to implement them with inline functions.

Since the above says that using inline functions is more efficient, I simply thank the code for verification

The test code is as follows:

StaticInlinevoidAddintXinty);intMainintargcConst Char*argv[])    {clock_t start, finish; Doubleduration; Start=clock ();  for(inti =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;}voidAddintXinty) {    intK = x+y; printf ("%d\n", K); }

The result of running three consecutive prints is:

0.086523 seconds
0.086504 seconds
0.085425 seconds

Then the inline function is changed to normal function and then run three times.

voidAddintXinty);intMainintargcConst Char*argv[])    {clock_t start, finish; Doubleduration; Start=clock ();  for(inti =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;}voidAddintXinty) {    intK = x+y; printf ("%d\n", K); }

Printing results:

0.085639 seconds
0.086934 seconds
0.085713 seconds

As you can see from the results, the use of inline functions in this test code can actually improve the speed of operation. So we can use inline functions moderately in the project later to improve app performance.

(inline) Use of inline functions in iOS development

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.