Function call and inline functions

Source: Internet
Author: User

Function call Overhead:

When a function is called, a function call and return are required. You need to save the context information of the current program so that after the function call is completed, the original location is returned and the program continues to be executed. Apply the function parameters to the stack, exit the stack, and execute the function. After the function is called, the memory occupied by the internal variables is released.

Inline:

Declaring a function as inline is a suggestion for the compiler. the compiler can choose not to execute inline. Most compilers perform inline during compilation. Therefore, the virtual function cannot be inline, because the compiler does not know which function to call during compilation. The compiler does not implement inlining for calling through function pointers.

Do not perform inline on constructor and destructor easily, because even if the function body is empty, the internal compiler will generate some code, such as adding a default constructor, by default, the constructor is copied.

When a function is declared as inline, the compiler does not regard it as a function, but similar to copying the function code to the original place, thus saving the overhead of function calling.

Limit most inlining to small and frequently called functions. Inline functions copy code in every place where a function is called, which will expand the generated code. For a function, the speed of obtaining this function will be very small or even not. Therefore, do not perform inline for recursion or loops.

The inline function should put the Declaration and definition together, otherwise it will have no effect. Function definitions within the class are automatically extended to inline. The inline statement must be explicitly added outside the class.

The inline function is usually placed in the header file.

Most debuggers are helpless with the inline function, because it is not easy to set breakpoints in a non-existent function.

 

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.