A random discussion on inline functions

Source: Internet
Author: User

The conclusion of the existence of inline functions is that:

      • Inline functions are introduced to solve the problem of function call efficiency
      • Because calls between functions are transferred from one memory address to another, the address of the original function execution is returned when the function call is complete. function calls have a certain amount of time overhead, and an inline function is introduced to solve this problem.
    • Without inline-modified functions, the call command appears in the assembly. Calling the Invoke command is required:
      • (1) Place the address of the next instruction in the stack
      • (2) and the starting address of the subroutine into the PC (so that the next instruction of the CPU will be transferred to execute the subroutine).
Reason
Because the calling function actually transfers the program execution order to an address in memory in the function, the program content of the function is executed and then returned to where it was before the function was moved to execute. This transfer operation requires that the site be protected before the transfer and memory of the execution address, back to the scene, and then continue to the original save address.

Motivation

Inline extensions are used to eliminate the time overhead of function calls. It is commonly used for frequently executed functions. A function of a small memory space is very beneficial.

Without inline functions, the compiler can decide which functions are inline. Programmers have little or no control over which functions are inline and which are not. To this degree of control, the role of the programmer can choose to inline specific applications

Effect
At the time of program compilation, the compiler replaces the call expression for an inline function that appears in the program with the function body of an inline function. Obviously, this kind of practice does not produce the problem of turning back, but because the code in the function Hugh is replaced in the program at compile time, it will increase the target program code and increase the space cost, and the time consignment is not as large as the function call, it can be seen that it is at the expense of the increase of the target code to save time.
1. Inline functions reduce the overhead of the CPU, and the overall speed of the program is accelerated, but when the inline function is large, it has the opposite effect, so the generally smaller function uses the inline function. 2. There are two methods of declaring inline functions, one is to use the inline closing word before the function, and the other is to define the code of the function inside the class, so that the function is automatically converted to an inline function, and there is no need to put the inline in front of the function. 3. Inline is a request to the compiler, and the following conditions prevent the compiler from obeying the request. If the function contains a loop, switch or goto statement, a recursive function that contains static functions. As you can see, inline functions and member functions are no different, the difference is how to speed up the execution of the function.
inline functions are a waste of space to save time, because a function call is a waste of time, and an inline function can replace a function call with a function body content at each invocation, somewhat like a macro definition. Inline functions can be used when the function body statement is small and there are no complex loop statements and the number of calls is high. 

Conclusion why can inline replace macros?
    • Advantages compared to functions:

      • 1) The inline function avoids the common function, in the assembly must invoke the disadvantage of call: Cancel the function of the parameter stack, reduce the cost of the call, improve efficiency. Therefore, the execution speed is faster than the normal function.

      • 2) Integration of the advantages of the macro, the use of direct replacement with the code (like a macro);

    • Advantages compared to macro:

      • 1) Avoid the disadvantages of macros: Pre-compilation is required. Because inline inline functions are also functions, you do not need to precompile.

      • 2) When an inline function is called, the compiler first checks the type of its arguments to ensure that the call is correct. Then perform a series of related checks, just like any real function. This eliminates its hidden dangers and limitations.

      • 3) You can use the protected members and private members of your class.

Description of inline inline functions
    • 1. The inline function is just the application we provide to the compiler, and the compiler does not necessarily take the inline form to invoke the function.
    • 2. Inline functions cannot host large amounts of code. If the function body of the inline function is too large, the compiler automatically discards the inline.
    • 3. Loop statements or switch statements are not allowed in inline functions.
    • 4. The definition of an inline function must precede the call.



Reference Link: 1.http://www.cnblogs.com/pengyingh/articles/2405718.html2.https://objccn.io/issue-6-2/

A random discussion on inline functions

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.