C++ gcc -O2 的含义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/655382/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 16:33:01  来源:igfitidea点击:

Meaning of gcc -O2

c++cgcc

提问by vartec

I see this flag a lot in the makefiles. What does it mean and when should it be used?

我在 makefile 中经常看到这个标志。它是什么意思,什么时候应该使用?

回答by Can Berk Güder

Optimization level 2.

优化级别 2。

From the GCC man page:

从 GCC 手册页:

-O1 Optimize. Optimizing compilation takes somewhat more time, and a lot more memory for a large function.

-O2 Optimize even more. GCC performs nearly all supported optimizations that do not involve a space-speed tradeoff. The compiler does not perform loop unrolling or function inlining when you specify -O2. As compared to -O, this option increases both compilation time and the performance of the generated code.

-O3 Optimize yet more. -O3 turns on all optimizations specified by -O2 and also turns on the -finline-functions, -funswitch-loops, -fpredictive-commoning, -fgcse-after-reload and -ftree-vectorize options.

-O0 Reduce compilation time and make debugging produce the expected results. This is the default.

-Os Optimize for size. -Os enables all -O2 optimizations that do not typically increase code size. It also performs further optimizations designed to reduce code size.

-O1 优化。优化编译需要更多的时间,对于大型函数需要更多的内存。

-O2 优化更多。GCC 执行几乎所有不涉及空间速度权衡的受支持优化。当您指定 -O2 时,编译器不会执行循环展开或函数内联。与 -O 相比,此选项会增加编译时间和生成代码的性能。

-O3 优化更多。-O3 打开 -O2 指定的所有优化,并打开 -finline-functions、-funswitch-loops、-fpredictive-commoning、-fgcse-after-reload 和 -ftree-vectorize 选项。

-O0 减少编译时间并使调试产生预期结果。这是默认设置。

-Os 优化大小。-Os 启用所有通常不会增加代码大小的 -O2 优化。它还执行旨在减少代码大小的进一步优化。

回答by vartec

Optimization level 2, max is 3. See: http://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html

优化级别 2,最大值为 3。请参阅:http: //gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html

Note, that in few years ago -O3could cause some glitches by excessively "optimizing" the code. AFAIK, that's no longer true with modern versions of GCC. But with inertia, -O2is considered "the max safe".

请注意,在几年前-O3,过度“优化”代码可能会导致一些故障。AFAIK,现代版本的 GCC 不再如此。但随着惯性,-O2被认为是“最大的安全”。

回答by schnaader

This is an optimize switch. See gcc --help.

这是一个优化开关。请参阅 gcc --help。

回答by Wadih M.

Compilers can use various optimization techniques like loop unrolling, CPU pipeline optimizations to find useless code and avoid data hazards to speed up your code. For example, a loop that happens a fixed amount of times will be converted to contiguous code without the loop control overhead. Or if all the loop iterations are independent, some code parallelization is possible.

编译器可以使用各种优化技术,例如循环展开、CPU 管道优化来查找无用的代码并避免数据危害以加速代码。例如,发生固定次数的循环将被转换为连续代码,而无需循环控制开销。或者,如果所有循环迭代都是独立的,则某些代码并行化是可能的。

Setting the optimization level to 2 tells how much energy the compiler should spend looking for those optimizations. The possible values range from 1 to 3

将优化级别设置为 2 表示编译器应该花费多少精力来寻找这些优化。可能的值范围从 1 到 3

You can learn more about what the compiler can do to optimize your code: http://en.wikipedia.org/wiki/Compiler_optimization

您可以了解更多关于编译器可以做什么来优化您的代码:http: //en.wikipedia.org/wiki/Compiler_optimization

回答by Mekk

Tried manpage?

尝试联机帮助页?

-O2

-O2

Optimize even more. GCC performs nearly all supported optimizations that do not involve a space-speed tradeoff. The compiler does not perform loop unrolling or function inlining when you specify -O2. As compared to -O, this option increases both compilation time and the performance of the generated code.

进一步优化。GCC 执行几乎所有不涉及空间速度权衡的受支持优化。当您指定 -O2 时,编译器不会执行循环展开或函数内联。与 -O 相比,此选项会增加编译时间和生成代码的性能。

In human words: it is the highest truly safe way of optimization. -O3 makes reorganizations which can be troublesome at times. The subject as such is fairly deep.

用人的话说:这是最高的真正安全的优化方式。-O3 进行重组,有时会很麻烦。这样的主题是相当深刻的。

回答by RYUZAKI

Without any optimization option, the compiler's goal is to reduce the cost of compilation and to make debugging produce the expected results. Turning on optimization makes the compiler attempt to improve the performance and/or code size at the expense of compilation time and possibly the ability to debug the program.

在没有任何优化选项的情况下,编译器的目标是降低编译成本并使调试产生预期的结果。启用优化会使编译器尝试以牺牲编译时间和调试程序的能力为代价来提高性能和/或代码大小。

The default is optimization off. This results in the fastest compile time, but the compiler makes absolutely no attempt to optimize, and the generated programs are considerably larger and slower than when optimization is enabled. There are various -O switches (the permitted forms are -O0, -O1 -O2, -O3, and -Os) in gcc to control the optimization level:

默认是优化关闭。这导致最快的编译时间,但编译器绝对不尝试优化,并且生成的程序比启用优化时更大更慢。gcc 中有各种 -O 开关(允许的形式是 -O0、-O1 -O2、-O3 和 -Os)来控制优化级别:

-O0 No optimization; generates unoptimized code but has the fastest compilation time. This is default.

-O0 无优化;生成未优化的代码,但编译时间最快。这是默认设置。

-O1 Moderate optimization; optimizes reasonably well but does not degrade compilation time significantly. It takes a lot more memory for large function.

-O1 适度优化;优化得相当好,但不会显着缩短编译时间。大型函数需要更多内存。

-O2 GCC performs nearly all supported optimizations that do not involve a space-speed tradeoff. The compiler does not perform loop unrolling or function inlining when you specify

-O2 GCC 执行几乎所有不涉及空间速度权衡的受支持优化。当您指定时,编译器不会执行循环展开或函数内联

-O3 Full optimization as in -O2; also uses more aggressive automatic inlining of subprograms within a unit and attempts to vectorize loops. It also turns on the -finline-functions, -funswitch-loops, -fpredictive-commoning, -fgcse-after-reload and -ftree-vectorize options.

-O3 与 -O2 中的完全优化;还使用更积极的自动内联单元内的子程序并尝试向量化循环。它还打开 -finline-functions、-funswitch-loops、-fpredictive-commoning、-fgcse-after-reload 和 -ftree-vectorize 选项。

-Os Optimize for size. -Os enables all -O2 optimizations that do not typically increase code size. It also performs further optimizations designed to reduce code size.

-Os 优化大小。-Os 启用所有通常不会增加代码大小的 -O2 优化。它还执行旨在减少代码大小的进一步优化。

To learn more about flags/options used at various optimization levels and their details: http://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html

要了解有关在各种优化级别使用的标志/选项及其详细信息的更多信息:http: //gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html