Use inline functions with caution

Source: Internet
Author: User

Inner join can improve the execution efficiency of functions. Why not define all functions as inline functions?

If all functions are inline functions, do you still need the keyword "inline?

Inline is at the cost of code expansion (replication). It only saves the overhead of function calls and improves the function execution efficiency. If the execution time of code in the function body is higher than the overhead of function calling, the efficiency gains will be little. On the other hand, every call to an inline function must copy the code, which will increase the total amount of code in the program and consume more memory space. Inline is not recommended in the following cases:

(1) If the code in the function body is long, using inline will cause high memory consumption.

(2) If there is a loop in the function body, the execution time of the Code in the function body is longer than the overhead of the function call.

Class constructor and destructor are easy to misunderstand that using inline is more effective. Be careful that constructors and destructor may hide some behaviors, such as secretly executing constructors and destructor of base classes or member objects. Therefore, do not place the constructor and destructor definitions in the class declaration.

A good compiler will automatically cancel the inline that is not worthwhile based on the definition body of the function (this further demonstrates that inline should not appear in the declaration of the 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.