我知道内联是对编译器的提示或请求,它用于避免函数调用开销。
那么在什么基础上可以确定一个函数是否是内联的候选者呢? 在哪种情况下应该避免内联?
最佳答案
避免函数调用的成本只是故事的一半。
做:
- 使用
inline
代替#define
- 非常小的函数非常适合
inline
:更快的代码和更小的可执行文件(更多的机会留在代码缓存中) - 函数很小并且经常被调用
不要:
- 大型函数:导致更大的可执行文件,无论调用开销导致执行速度如何,都会显着降低性能
- 受 I/O 限制的内联函数
- 该功能很少使用
- 构造函数和析构函数:即使为空,编译器也会为它们生成代码
- 在开发库时破坏二进制兼容性:
- 内联现有函数
- 更改内联函数或使内联函数非内联:库的早期版本调用旧实现
在开发库时,为了使类在未来可扩展,您应该:
- 即使主体为空,也可以添加非内联虚拟析构函数
- 使所有构造函数非内联
- 编写复制构造函数和赋值运算符的非内联实现,除非类不能按值复制
请记住,inline
关键字是对编译器的提示:编译器可能决定不内联函数,它可以决定内联未标记为 inline
的函数首先。我通常避免标记函数 inline
(可能在编写非常非常小的函数时除外)。
关于性能,明智的做法是(一如既往)分析应用程序,然后最终内联
一组代表瓶颈的函数。
引用资料:
- To Inline or Not To Inline
- [9] Inline functions
- Policies/Binary Compatibility Issues With C++
- GotW #33: Inline
- Inline Redux
- 有效的 C++ - 第 33 条:明智地使用内联
编辑:Bjarne Stroustrup,C++ 编程语言:
A function can be defined to be
inline
. For example:
inline int fac(int n)
{
return (n < 2) ? 1 : n * fac(n-1);
}
The
inline
specifier is a hint to the compiler that it should attempt to generate code for a call offac()
inline rather than laying down the code for the function once and then calling through the usual function call mechanism. A clever compiler can generate the constant720
for a callfac(6)
. The possibility of mutually recursive inline functions, inline functions that recurse or not depending on input, etc., makes it impossible to guarantee that every call of aninline
function is actually inlined. The degree of cleverness of a compiler cannot be legislated, so one compiler might generate720
, another6 * fac(5)
, and yet another an un-inlined callfac(6)
.To make inlining possible in the absence of unusually clever compilation and linking facilities, the definition–and not just the declaration–of an inline function must be in scope (§9.2). An
inline
especifier does not affect the semantics of a function. In particular, an inline function still has a unique address and so hasstatic
variables (§7.1.2) of an inline function.
EDIT2:ISO-IEC 14882-1998,7.1.2 功能说明符
A function declaration (8.3.5, 9.3, 11.4) with an
inline
specifier declares an inline function. The inline specifier indicates to the implementation that inline substitution of the function body at the point of call is to be preferred to the usual function call mechanism. An implementation is not required to perform this inline substitution at the point of call; however, even if this inline substitution is omitted, the other rules for inline functions defined by 7.1.2 shall still be respected.
关于c++ - 什么时候使用内联函数,什么时候不使用它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1932311/