在c++中使用内联函数的优点/缺点是什么?我看到它只提高了编译器输出的代码的性能,但随着今天优化的编译器,快速的cpu,巨大的内存等(不像在1980年<内存是稀缺的,所有东西都必须适合100KB内存),他们今天真正有什么优势?
当前回答
Generally speaking, these days with any modern compiler worrying about inlining anything is pretty much a waste of time. The compiler should actually optimize all of these considerations for you through its own analysis of the code and your specification of the optimization flags passed to the compiler. If you care about speed, tell the compiler to optimize for speed. If you care about space, tell the compiler to optimize for space. As another answer alluded to, a decent compiler will even inline automatically if it really makes sense.
另外,正如其他人所说,使用内联并不能保证任何东西都是内联的。如果你想要保证它,你必须定义一个宏而不是一个内联函数来实现它。
何时内联和/或定义宏强制包含?-只有当你对关键代码段的速度有了证明和必要的提高,并且知道这对应用程序的整体性能有影响时。
其他回答
内联函数是编译器使用的优化技术。可以简单地在函数原型前加上inline关键字来使函数内联。内联函数指示编译器在代码中使用函数的任何地方插入完整的函数体。
优点:-
It does not require function calling overhead. It also save overhead of variables push/pop on the stack, while function calling. It also save overhead of return call from a function. It increases locality of reference by utilizing instruction cache. After in-lining compiler can also apply intra-procedural optimization if specified. This is the most important one, in this way compiler can now focus on dead code elimination, can give more stress on branch prediction, induction variable elimination etc..
要了解更多信息,可以点击这个链接 http://tajendrasengar.blogspot.com/2010/03/what-is-inline-function-in-cc.html
这并不完全是关于性能。c++和C都用于嵌入式编程,位于硬件之上。例如,如果您要编写中断处理程序,则需要确保代码可以立即执行,而不会交换额外的寄存器和/或内存页。这就是内联派上用场的时候。好的编译器在需要速度时自己做一些“内联”,但“内联”迫使它们这样做。
Why not make all functions inline by default? Because it's an engineering trade off. There are at least two types of "optimization": speeding up the program and reducing the size (memory footprint) of the program. Inlining generally speeds things up. It gets rid of the function call overhead, avoiding pushing then pulling parameters from the stack. However, it also makes the memory footprint of the program bigger, because every function call must now be replaced with the full code of the function. To make things even more complicated, remember that the CPU stores frequently used chunks of memory in a cache on the CPU for ultra-rapid access. If you make the program's memory image big enough, your program won't be able to use the cache efficiently, and in the worst case inlining could actually slow your program down. To some extent the compiler can calculate what the trade offs are, and may be able to make better decisions than you can, just looking at the source code.
内联函数更快,因为你不需要将参数和返回地址等东西推入或弹出堆栈;但是,它确实会使二进制文件略大一些。
有显著的区别吗?对大多数人来说,在现代硬件上还不够明显。但它可以产生影响,这对一些人来说已经足够了。
将某些东西标记为内联并不能保证它将是内联的。这只是给编译器的一个建议。有时候是不可能的比如你有一个虚函数,或者涉及到递归。有时候编译器会选择不使用它。
我可以看到这样的情况会产生明显的不同:
inline int aplusb_pow2(int a, int b) {
return (a + b)*(a + b) ;
}
for(int a = 0; a < 900000; ++a)
for(int b = 0; b < 900000; ++b)
aplusb_pow2(a, b);
在优化过程中,许多编译器会内联函数,即使你没有标记它们。如果你知道一些编译器不知道的东西,你通常只需要将函数标记为内联,因为它自己通常可以做出正确的决定。
推荐文章
- 为什么我的程序不能在Windows 7下用法语编译?
- 如何获取变量的类型?
- 什么是奇怪的重复模板模式(CRTP)?
- 连接两个向量的最佳方法是什么?
- 在c++中,是通过值传递更好,还是通过引用到const传递更好?
- 在STL中deque到底是什么?
- Windows上最好的免费c++分析器是什么?
- 如何自动转换强类型枚举为int?
- 在一个类中使用具有成员函数的泛型std::function对象
- 'for'循环中的后增量和前增量产生相同的输出
- 虚函数和纯虚函数的区别
- c++中的_tmain()和main()有什么区别?
- 内存泄漏是否正常?
- 当启用c++ 11时,std::vector性能回归
- 什么时候使用哪种指针?