知乎上有篇詳解 https://zhuanlan.zhihu.com/p/24402180 強烈推薦
C/C++的優化等級會對程序產生 不定性的影響,至於選擇哪種優化等級必須從 現有的程序分析才行
可參考知乎上的講解
原文
0 Minimum optimization. Turns off most optimizations.It gives the best possible debug view and the lowest level of optimization.
近乎不優化,用於調試代碼。出現代碼行不能設置斷點可如此設置試試。
1 Restrictedoptimization. Removes unused inline functions and unused static functions.Turns off optimizations that seriously degrade the debug view. Ifused with --debug, this option gives a satisfactorydebug view with good code density.
部分優化。移除未調用的內聯函數和靜態函數,關閉debug窗口優化,此狀態也能用於調試
2 Highoptimization. If used with --debug, the debug viewmight be less satisfactory because the mapping of object code tosource code is not always clear.
This is the default optimization level.
默認優化等級。如果處於debug狀態,部分代碼行將不能被調試,具體做了什么優化好像沒說
3 Maximumoptimization. -O3 performs the same optimizationsas -O2 however the balance between space and timeoptimizations in the generated code is more heavily weighted towardsspace or time compared with -O2. That is:
-O3 -Otime aims to produce fastercode than -O2 -Otime, at the risk of increasingyour image size
-O3 -Ospace aims to produce smallercode than -O2 -Ospace, but performance might bedegraded.
In addition, -O3 performs extra optimizationsthat are more aggressive, such as:
High-levelscalar optimizations, including loop unrolling, for -O3 -Otime. Thiscan give significant performance benefits at a small code size cost,but at the risk of a longer build time.
More aggressive inlining and automatic inliningfor -O3 -Otime.
-O0
最少的優化,可以最大程度上配合產生代碼調試信息,可以在任何代碼行打斷點,特別是死代碼處。
-O1
有限的優化,去除無用的inline和無用的static函數、死代碼消除等,在影響到調試信息的地方均不進行優化。在適當的代碼體積和充分的調試之間平衡,代碼編寫階段最常用的優化等級。
-O2
高度優化,調試信息不友好,有可能會修改代碼和函數調用執行流程,自動對函數進行內聯等。
-O3
最大程度優化,產生極少量的調試信息。會進行更多代碼優化,例如循環展開,更激進的函數內聯等。
另外,可以通過單獨設置 --loop_optimization_level=option 來控制循環展開的優化等級。
原文鏈接:https://blog.csdn.net/liuqi3256797/java/article/details/90137350