跳到正文

个人记录

氧气优化

神之右大臣约 5 分钟
原文
文章信息与版本

文章信息

发布
2019年2月17日
收录
2025年12月12日
更新
2026年11月18日
篇幅
约 5 分钟

版本

  1. v1当前

重点:如果使用多个-O选项(包含或不包含级别编号),则最后一个选项是有效的选项。



例如: #pragma GCC optimize(2) #pragma G++ optimize(2) 这就是传说中的氧气优化


-O、-O1:优化编译需要更多时间,并且大型函数需要更多内存。使用-O选项,编译器会尝试减小代码尺寸减少执行时间,不执行任何需要大量编译时间的优化。

-O选项打开了如下优化标志:

-fauto-inc-dec -fbranch-count-reg -fcombine-stack-adjustments -fcompare-elim -fcprop-registers -fdce -fdefer-pop -fdelayed-branch -fdse -fforward-propagate -fguess-branch-probability -fif-conversion2 -fif-conversion -finline-functions-called-once -fipa-pure-const -fipa-profile -fipa-reference -fmerge-constants -fmove-loop-invariants -fshrink-wrap -fsplit-wide-types -ftree-bit-ccp -ftree-ccp -fssa-phiopt -ftree-ch -ftree-copy-prop -ftree-copyrename -ftree-dce -ftree-dominator-opts -ftree-dse -ftree-forwprop -ftree-fre -ftree-phiprop -ftree-sink -ftree-slsr -ftree-sra -ftree-pta -ftree-ter -funit-at-a-time


-O2:相对-O优化更多。 GCC几乎执行所有支持的优化,但不涉及空速权衡。 与-O相比,此选项增加了编译时间和生成代码的性能。

-O2除了打开所有-O指定的优化标志,还打开了如下优化标志:

-fthread-jumps -falign-functions -falign-jumps -falign-loops -falign-labels -fcaller-saves -fcrossjumping -fcse-follow-jumps -fcse-skip-blocks -fdelete-null-pointer-checks -fdevirtualize -fdevirtualize-speculatively -fexpensive-optimizations -fgcse -fgcse-lm -fhoist-adjacent-loads -finline-small-functions -findirect-inlining -fipa-cp -fipa-cp-alignment -fipa-sra -fipa-icf -fisolate-erroneous-paths-dereference -flra-remat -foptimize-sibling-calls -foptimize-strlen -fpartial-inlining -fpeephole2 -freorder-blocks -freorder-blocks-and-partition -freorder-functions -frerun-cse-after-loop -fsched-interblock -fsched-spec -fschedule-insns -fschedule-insns2 -fstrict-aliasing -fstrict-overflow -ftree-builtin-call-dce -ftree-switch-conversion -ftree-tail-merge -ftree-pre -ftree-vrp -fipa-ra


-O3:相对-O2优化更多。

-O3除了打开所有-O2指定的优化标志,还打开了如下优化标志:

-finline-functions, -funswitch-loops, -fpredictive-commoning, -fgcse-after-reload, -ftree-loop-vectorize, -ftree-loop-distribute-patterns, -ftree-slp-vectorize, -fvect-cost-model, -ftree-partial-pre and -fipa-cp-clone


-O0:减少编译时间并使调试可以产生预期的结果,这也是默认的优化等级。


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

-Os关闭如下优化标志:

-falign-functions -falign-jumps -falign-loops -falign-labels -freorder-blocks -freorder-blocks-and-partition -fprefetch-loop-arrays


-Ofast:无视严格的标准合规性。 -Ofast启用所有-O3优化。 它还打开并非对所有符合标准的程序有效的优化。 它打开

-ffast-math和Fortran特定的-fno-protect-parens和-fstack-arrays。


Og:优化调试体验。 -Og启用不会干扰调试的优化。 它是标准编辑 - 编译 - 调试周期可以选择的优化级别,提供合理的优化级别,同时保持快速编译和良好的调试体验

原文