问题描述
在编译 C++ 代码时,GCC 是否曾尝试通过选择内联未用 inline
关键字标记的函数来优化速度?
Does GCC, when compiling C++ code, ever try to optimize for speed by choosing to inline functions that are not marked with the inline
keyword?
推荐答案
是的.任何编译器只要认为它是一个好主意,都可以自由地内联任何函数.GCC 也这样做.
Yes. Any compiler is free to inline any function whenever it thinks it is a good idea. GCC does that as well.
在 -O2
优化级别,内联在编译器认为值得做(使用启发式)并且如果它不会增加代码.在 -O3
处,只要编译器认为值得做,就会执行,而不管它是否会增加代码的大小.此外,在所有优化级别(即启用的优化)中,内联只调用一次的静态函数.
At -O2
optimization level the inlining is done when the compiler thinks it is worth doing (a heuristic is used) and if it will not increase the size of the code. At -O3
it is done whenever the compiler thinks it is worth doing, regardless of whether it will increase the size of the code. Additionally, at all levels of optimization (enabled optimization that is), static functions that are called only once are inlined.
正如下面的评论中所指出的,这些 -Ox
实际上是包含多个更具体设置的复合设置,包括与内联相关的设置(如 -finline-functions
和这样),所以我们也可以根据那些更具体的设置来描述行为(并控制它).
As noted in the comments below, these -Ox
are actually compound settings that envelop multiple more specific settings, including inlining-related ones (like -finline-functions
and such), so one can also describe the behavior (and control it) in terms of those more specific settings.
这篇关于GCC 内联 C++ 函数是否没有“inline"关键字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!