This question already has an answer here:
这个问题在这里已有答案:
- What is the difference between the /Ox and /O2 compiler options? 2 answers
- / Ox和/ O2编译器选项有什么区别? 2个答案
I've been trying to read through the MSDN pages on the various optimization flags.
我一直试图通过MSDN页面阅读各种优化标志。
We currently have most of our projects set to /O2
which optimizes for "maximize speed".
我们目前将大部分项目设置为/ O2,以优化“最大化速度”。
My confusion is what exactly this means. Which of the following statements are closer to true regarding the /O2
flag?
我的困惑是这究竟是什么意思。关于/ O2标志,下列哪一项陈述更接近真实?
- Optimize the code for both speed and size, but if there is contention prefer optimization for speed
- 针对速度和大小优化代码,但是如果存在争用,则优选速度优化
- Optimize the code only for speed, do not optimize for size.
- 仅针对速度优化代码,不优化大小。
I made the argument that we should use the /Ox
flag, but that was when I was under the impression that Option 2 was true.
我提出我们应该使用/ Ox标志的论点,但那时我认为选项2是真的。
I was basically told "we're not going to change from /O2
to /Ox
unless someone has solid evidence that we need to do so".
我基本上被告知“我们不会从/ O2改为/ Ox,除非有人有确凿的证据证明我们需要这样做”。
So my question is does /O2
still perform memory optimizations? E.g. return value optimization, copy elision, etc. What would we gain from switching from /O2
to /Ox
?
所以我的问题是/ O2仍然执行内存优化吗?例如。返回值优化,复制省略等。从/ O2切换到/ Ox会得到什么?
3 个解决方案
#1
6
As Arkanosis pointed out correctly, when going from /O2 to /Ox, you disable /Gs, /GF, /Gy. The question is which of these flags may increase execution speed?
正如Arkanosis指出的那样,当从/ O2转到/ Ox时,你禁用/ Gs,/ GF,/ Gy。问题是这些标志中的哪一个可能会提高执行速度?
/Gs is identical to /Gs0 and can have negative impact on performance. See below the description on MSDN.
/ Gs与/ Gs0相同,可能会对性能产生负面影响。请参阅下面MSDN上的说明。
activates stack probes for every function call that requires storage for local variables. This can have a negative impact on performance
为每个需要存储局部变量的函数调用激活堆栈探测器。这可能会对性能产生负面影响
/GF eliminates duplicate strings (constants) - called string pooling. This will reduce the code size. A lower code could produce lower number of instruction cache misses but I doubt this effect is observable on most codes.
/ GF消除了重复的字符串(常量) - 称为字符串池。这将减少代码大小。较低的代码可以产生较少数量的指令高速缓存未命中但我怀疑这种效果在大多数代码上是可观察的。
/Gy flag alows packaging individual functions into COMDAT structures. These can be used as a workaround to avoid compile time errors due to multiple definitions of the same symbol. The MSDN documentation states that this just affects build time but not the execution time. They generally recommend using it.
/ Gy flag将各个函数打包成COMDAT结构。这些可用作解决方法,以避免由于同一符号的多个定义而导致的编译时错误。 MSDN文档声明这只会影响构建时间,但不会影响执行时间。他们通常建议使用它。
Conclusion:
结论:
/Ox disables /Gs, /GF, /Gy. In some cases, these options hurt performance and almost never improve execution speed, compared with /O2. Of course they have benefits but not related to speed.
/ Ox禁用/ Gs,/ GF,/ Gy。在某些情况下,与/ O2相比,这些选项会影响性能并且几乎不会提高执行速度。当然,他们有好处,但与速度无关。
#2
5
-
/02
is the same as/Og /Oi /Ot /Oy /Ob2 /Gs /GF /Gy
- / 02与/ Og / Oi / Ot / Oy / Ob2 / Gs / GF / Gy相同
-
/Ox
is the same as/Og /Oi /Ot /Oy /Ob2
- / Ox与/ Og / Oi / Ot / Oy / Ob2相同
So switching from /O2
to /Ox
means:
因此从/ O2切换到/ Ox意味着:
- no
/Gs
(no controls stack probes) - no / Gs(无控件堆栈探针)
- no
/GF
(no string pooling) - 没有/ GF(没有字符串池)
- no
/Gy
(no function-level linking) - no / Gy(没有功能级链接)
#3
0
So my question is does /O2 still perform memory optimizations?
所以我的问题是/ O2仍然执行内存优化吗?
Depends what you mean by memory optimization.
取决于内存优化的含义。
/O1 is guaranteed to optimize the code based on binary size.
/ O1保证基于二进制大小优化代码。
Creates the smallest code in the majority of cases.
在大多数情况下创建最小的代码。
/O2 primarily aims to optimize code for faster speed.
/ O2主要旨在优化代码以加快速度。
Creates the fastest code in the majority of cases. (default setting for release builds)
在大多数情况下创建最快的代码。 (发布版本的默认设置)
Cases when /O2 is orthogonal to /O1 (not limited) when
当/ O2与/ O1正交(不限)时的情况
- compiler does loop unrolling,
- 编译器循环展开,
- code reordering
- 代码重新排序
- code inlining
- 代码内联
Now consider /Ox, it produces code that favors execution speed over smaller size, so /Ox does not includes /Os (Favor Small Code) minimizes the size of EXEs and DLLs by instructing the compiler to favor size over speed.
现在考虑/ Ox,它生成的代码有利于执行速度超过较小的大小,因此/ Ox不包括/ Os(优惠小代码)通过指示编译器支持大小超过速度来最小化EXE和DLL的大小。
#1
6
As Arkanosis pointed out correctly, when going from /O2 to /Ox, you disable /Gs, /GF, /Gy. The question is which of these flags may increase execution speed?
正如Arkanosis指出的那样,当从/ O2转到/ Ox时,你禁用/ Gs,/ GF,/ Gy。问题是这些标志中的哪一个可能会提高执行速度?
/Gs is identical to /Gs0 and can have negative impact on performance. See below the description on MSDN.
/ Gs与/ Gs0相同,可能会对性能产生负面影响。请参阅下面MSDN上的说明。
activates stack probes for every function call that requires storage for local variables. This can have a negative impact on performance
为每个需要存储局部变量的函数调用激活堆栈探测器。这可能会对性能产生负面影响
/GF eliminates duplicate strings (constants) - called string pooling. This will reduce the code size. A lower code could produce lower number of instruction cache misses but I doubt this effect is observable on most codes.
/ GF消除了重复的字符串(常量) - 称为字符串池。这将减少代码大小。较低的代码可以产生较少数量的指令高速缓存未命中但我怀疑这种效果在大多数代码上是可观察的。
/Gy flag alows packaging individual functions into COMDAT structures. These can be used as a workaround to avoid compile time errors due to multiple definitions of the same symbol. The MSDN documentation states that this just affects build time but not the execution time. They generally recommend using it.
/ Gy flag将各个函数打包成COMDAT结构。这些可用作解决方法,以避免由于同一符号的多个定义而导致的编译时错误。 MSDN文档声明这只会影响构建时间,但不会影响执行时间。他们通常建议使用它。
Conclusion:
结论:
/Ox disables /Gs, /GF, /Gy. In some cases, these options hurt performance and almost never improve execution speed, compared with /O2. Of course they have benefits but not related to speed.
/ Ox禁用/ Gs,/ GF,/ Gy。在某些情况下,与/ O2相比,这些选项会影响性能并且几乎不会提高执行速度。当然,他们有好处,但与速度无关。
#2
5
-
/02
is the same as/Og /Oi /Ot /Oy /Ob2 /Gs /GF /Gy
- / 02与/ Og / Oi / Ot / Oy / Ob2 / Gs / GF / Gy相同
-
/Ox
is the same as/Og /Oi /Ot /Oy /Ob2
- / Ox与/ Og / Oi / Ot / Oy / Ob2相同
So switching from /O2
to /Ox
means:
因此从/ O2切换到/ Ox意味着:
- no
/Gs
(no controls stack probes) - no / Gs(无控件堆栈探针)
- no
/GF
(no string pooling) - 没有/ GF(没有字符串池)
- no
/Gy
(no function-level linking) - no / Gy(没有功能级链接)
#3
0
So my question is does /O2 still perform memory optimizations?
所以我的问题是/ O2仍然执行内存优化吗?
Depends what you mean by memory optimization.
取决于内存优化的含义。
/O1 is guaranteed to optimize the code based on binary size.
/ O1保证基于二进制大小优化代码。
Creates the smallest code in the majority of cases.
在大多数情况下创建最小的代码。
/O2 primarily aims to optimize code for faster speed.
/ O2主要旨在优化代码以加快速度。
Creates the fastest code in the majority of cases. (default setting for release builds)
在大多数情况下创建最快的代码。 (发布版本的默认设置)
Cases when /O2 is orthogonal to /O1 (not limited) when
当/ O2与/ O1正交(不限)时的情况
- compiler does loop unrolling,
- 编译器循环展开,
- code reordering
- 代码重新排序
- code inlining
- 代码内联
Now consider /Ox, it produces code that favors execution speed over smaller size, so /Ox does not includes /Os (Favor Small Code) minimizes the size of EXEs and DLLs by instructing the compiler to favor size over speed.
现在考虑/ Ox,它生成的代码有利于执行速度超过较小的大小,因此/ Ox不包括/ Os(优惠小代码)通过指示编译器支持大小超过速度来最小化EXE和DLL的大小。