Inline Functions
Inline functions
Includefine functions inline only when they are small, say, 10 lines or less.
It is defined as inline only when the function is very small and within 10 rows.
Definition: You can declare functions in a way that allows the compiler to expand them inline rather than calling them through the usual function call mechanism.
Definition: A function call mechanism. If you declare a function as inline, the compiler will expand the entire function body in the call.
Pros: Inlining a function can generate more efficient object code, as long as the inlined function is small. Feel free to inline accessors and mutators, and other short, performance-critical functions.
Prerequisites: When inline is a small function, code is generated more efficiently. Consider class members accessing functions or other short functions with strict performance requirements.
Cons: Overuse of inlining can actually make programs slower. depending on a function's size, inlining it can cause the code size to increase or decrease. inlining a very small accessor function will usually decrease code size while inlining a very large function can dramatically increase code size. on modern processors smaller code usually runs faster due to better use of the instruction cache.
Restriction: excessive use of inline functions actually slows down the program running, depending on the size of the function body, which determines the increase or decrease of code. Inline accessing a function by a very small Member reduces code, while Inline Function Code increases dramatically. Since instruction caching is widely used on modern processors, small code usually runs faster.
Demo:
Decision Making:
A decent rule of thumb is to not inline a function if it is more than 10 lines long. beware of destructors, which are often longer than they appear because of implicit member-and base-destructor cballs!
A suitable empirical rule: Do not inline a function with more than 10 rows, but be careful with the destructor. It does not seem as small as it looks, it calls class members and destructor of the base class. Www.2cto.com
Another useful rule of thumb: it's typically not cost valid tive to inline functions with loops or switch statements (unless, in the common case, the loop or switch statement is never executed ).
Other useful empirical rules: a function is usually inefficient if there is a judgment or loop statement. (Unless loop statements or judgment statements are not executed normally ).
It is important to know that functions are not always inlined even if they are declared as such; for example, virtual and recursive functions are not normally inlined. usually recursive functions shoshould not be inline. the main reason for making a virtual function inline is to place its definition in the class, either for convenience or to document its behavior, e.g ., for accessors and mutators.
It is important to know which functions do not inline even if you declare inline: for example, virtual and recursive functions do not inline normally. In general, recursive functions should not be inline. The main reason for the inline function is the class definition, which is mainly the cause of convenience, for example, the member accesses the function.