Inline function background, examples, differences from common functions and places to be noted------new standard C + + programming

Source: Internet
Author: User

background:

Using functions avoids the hassle of having to weigh the same code more than once, but also reduces the volume of executable programs, but also costs the program's running time. When a function call executes, it first allocates storage space for the parameter and local variables in the stack, and then copies the value of the argument to the parameter, and then the return address of the function (the address indicates where the program should go back to execution after the function is executed), and then jumps to the inside of the function. This process is time-consuming. In addition, when the function executes the return statement, it takes time to reclaim the storage space occupied by the parameters and local variables from the stack, then take the return address from the stack and jump to that address to continue execution. In short, the use of function call statements and directly to the code in the function to re-transcribe, save manpower, but bring the program run time overhead, in general, this overhead can be ignored. However, if there are few statements inside a function and the execution time is very short, the extra overhead generated by the function call is not negligible compared to the time the function itself executes. If such a function is executed thousands of times in a loop, the time overhead caused by a function call can make the program run noticeably slower. As a special focus on program execution efficiency, suitable for writing the underlying system software language, C + + with the inline keyword to better solve the problem of function call overhead.

Example:

In C + +, you can precede a return value type with an inline type word when defining a function. Such as:

inline int Min (int a,int b) {    if (a>b) return A;    return b;}

The function that adds the inline keyword is called an inline function.

Difference: 

When the compiler processes a statement that invokes an inline function, it does not compile the statement into the instruction of the function call, but instead inserts the code of the entire function body directly into the statement as if the entire function body was rewritten at the call. With inline functions, you can easily reuse a piece of code as you would call a function without having to pay the extra overhead of executing a function call. It is clear that the use of inline functions increases the volume of the final executable program. Saving time by changing space by time or by increasing space consumption is a common method in computer science.

Attention:

1 . The code in the inline function should be just a simple, fast execution of several statements. If a function is more complex, the time he executes may be more than the extra cost of a function call, then the result of processing it as an inline function is to give the code a lot of cost, but only to increase the speed of 1 per thousand, which is obviously not cost-effective. Sometimes the function looks very simple, for example, there is only one loop that contains one or two statements, but the number of executions of the loop can be many, and it consumes a lot of time, and this is not an appropriate way to implement it as an inline function.

2 . The declaration of an inline function must already have the definition of an inline function (that is, the entire function body), not just the declarations of the inline function.

New standard C + + programming

Inline function background, examples, differences from common functions and places to be noted------new standard C + + programming

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.