C++primer——inline 函数避免函数调用的开销
2014-04-22 16:45
253 查看
7.6. Inline Functions7.6. 内联函数Recall the function we wrote on page248 that returned a reference to the shorter of its two string parameters: 回顾在第 7.3.2 节编写的那个返回两个string 形参中较短的字符串的函数: // find longer of two strings const string &shorterString(const string &s1, const string &s2) { return s1.size() < s2.size() ? s1 : s2; } The benefits of defining a function for such a small operation include: 为这样的小操作定义一个函数的好处是: It is easier to read and understand a call to shorterString than it would be to read and interpret an expression that used the equivalent conditional expression in place of the function call. 阅读和理解函数 shorterString 的调用,要比读一条用等价的条件表达式取代函数调用表达式并解释它的含义要容易得多。 If a change needs to be made, it is easier to change the function than to find and change every occurrence of the equivalent expression.果需要做任何修改,修改函数要比找出并修改每一处等价表达式容易得多。 Using a function ensures uniform behavior. Each test is guaranteed to be implemented in the same manner. 使用函数可以确保统一的行为,每个测试都保证以相同的方式实现。 The function can be reused rather than rewritten for other applications. 函数可以重用,不必为其他应用重写代码。 There is, however, one potential drawback to making shorterString a function: Calling a function is slower than evaluating the equivalent expression. On most machines, a function call does a lot of work: registers are saved before the call and restored after the return; the arguments are copied; and the program branches to a new location. 但是,将 shorterString 写成函数有一个潜在的缺点:调用函数比求解等价表达式要慢得多。在大多数的机器上,调用函数都要做很多工作;调用前要先保存寄存器,并在返回时恢复;复制实参;程序还必须转向一个新位置执行。 inline Functions Avoid Function Call Overheadinline 函数避免函数调用的开销A function specified as inline (usually) is expanded "in line" at each point in the program in which it is invoked. Assuming we madeshorterString aninline function, then this call将函数指定为 inline 函数,(通常)就是将它在程序中每个调用点上“内联地”展开。假设我们将shorterString 定义为内联函数,则调用: cout << shorterString(s1, s2) << endl; would be expanded during compilation into something like 在编译时将展开为: cout << (s1.size() < s2.size() ? s1 : s2) << endl; The run-time overhead of making shorterString a function is thus removed. 从而消除了把 shorterString 写成函数的额外执行开销。 We can define shorterString as an inline function by specifying the keywordinline before the function's return type: 从而消除了把 shorterString 写成函数的额外执行开销。 // inline version: find longer of two strings inline const string & shorterString(const string &s1, const string &s2) { return s1.size() < s2.size() ? s1 : s2; }
一般来说,内联机制适用于优化小的、只有几行的而且经常被调用的函数。大多数的编译器都不支持递归函数的内联。一个 1200 行的函数也不太可能在调用点内联展开。 Put inline Functions in Header Files把 inline 函数放入头文件
inline 函数的定义对编译器而言必须是可见的,以便编译器能够在调用点内联展开该函数的代码。此时,仅有函数原型是不够的。 An inline function may be defined more than once in a program as long as the definition appears only once in a given source file and the definition is exactly the same in each source file. By putting inline functions in headers, we ensure that the same definition is used whenever the function is called and that the compiler has the function definition available at the point of call. inline 函数可能要在程序中定义不止一次,只要inline 函数的定义在某个源文件中只出现一次,而且在所有源文件中,其定义必须是完全相同的。把inline 函数的定义放在头文件中,可以确保在调用函数时所使用的定义是相同的,并且保证在调用点该函数的定义对编译器可见。
|
相关文章推荐
- C++箴言:避免析构函数调用虚函数
- C++箴言:避免析构函数调用虚函数
- C++箴言:避免析构函数调用虚函数
- C++箴言:避免构造或析构函数中调用虚函数
- C++箴言:避免析构函数调用虚函数
- C++箴言:避免析构函数调用虚函数
- C++箴言:避免析构函数调用虚函数
- C++箴言:避免构造或析构函数中调用虚函数 zz
- C++箴言:避免析构函数调用虚函数
- c++ inline函数和宏、函数调用
- C++箴言:避免析构函数调用虚函数 转载
- C++中构造函数和析构函数避免调用虚函数的问题
- C++箴言:避免析构函数调用虚函数
- C++中构造函数和析构函数避免调用虚函数的问题
- C++中构造函数和析构函数避免调用虚函数的问题 转载
- C++中构造函数和析构函数避免调用虚函数的问题
- C++中构造函数和析构函数避免调用虚函数的问题
- [C++] 函数调用开销
- C++箴言:避免析构函数调用虚函数
- C++箴言:避免析构函数调用虚函数