編譯優化 Optimization Level

這是我參與更文挑戰的第2天,活動詳情查看: 更文挑戰前端

編譯連接自己是一個很大的體系內容,咱們後續再開文章專門詳細說明;後端

關於編譯期優化

咱們都知道OC是一門編譯型語言,Xcode內置的編譯器已經從GCC轉換爲LLVMLLVM又分爲前端Clang和後端LLVM,爲了提升編譯後的機器碼執行效率,編譯器會在編譯過程當中把一些編譯期就能夠肯定的代碼進行優化,從而提升執行效率;markdown

例如app

int a = 5;
int b = 3;
int c = a + b;
複製代碼

這幾行代碼在通過優化後可能就會合併成一句代碼了,從彙編層面能夠很清楚的看到未優化前指令如圖所示, 4.1.pngide

當設置優化後的指令如圖所示函數

4.2.jpg

能夠看到:oop

  • 未優化前,先將5和3分別讀入寄存器,進行add運算後再寫回x0寄存器進行使用;
  • 優化後,編譯階段已經將結果8計算好了,運行階段直接將8讀入寄存器進行使用;

iOS 端的編譯優化設置Build Settings > Optimization Level,設置項的解釋以下post

官方解釋

Specifies the degree to which the generated code is optimized for speed and binary size.性能

None[-O0]: Do not optimize. With this setting, the compiler's goal is to reduce the cost of compilation and to make debugging produce the expected results. Statements are independent: if you stop the program with a breakpoint between statements, you can then assign a new value to any variable or change the program counter to any other statement in the function and get exactly the results you would expect from the source code. Fast[-O1]: Optimizing compilation takes somewhat more time, and a lot more memory for a large function. With this setting, the compiler tries to reduce code size and execution time, without performing any optimizations that take a great deal of compilation time. In Apple's compiler, strict aliasing, block reordering, and inter-block scheduling are disabled by default when optimizing. Faster[-O2]: The compiler performs nearly all supported optimizations that do not involve a space-speed tradeoff. With this setting, the compiler does not perform loop unrolling or function inlining, or register renaming. As compared to the 'Fast' setting, this setting increases both compilation time and the performance of the generated code. Fastest[-O3]: Turns on all optimizations specified by the 'Faster' setting and also turns on function inlining and register renaming options. This setting may result in a larger binary. Fastest, Smallest[-Os]: Optimize for size. This setting enables all 'Faster' optimizations that do not typically increase code size. It also performs further optimizations designed to reduce code size. Fastest, Aggressive Optimizations[-Ofast]: This setting enables 'Fastest' but also enables aggressive optimizations that may break strict standards compliance but should work well on well-behaved code.優化

中文翻譯

  • None[-O0]: 不優化。在這種設置下, 編譯器的目標是下降編譯消耗,保證調試時輸出指望的結果。程序的語句之間是獨立的:若是在程序的停在某一行的斷點出,咱們能夠給任何變量賦新值抑或是將程序計數器指向方法中的任何一個語句,而且能獲得一個和源碼徹底一致的運行結果。
  • Fast[-O1]: 大函數所需的編譯時間和內存消耗都會稍微增長。在這種設置下,編譯器會嘗試減少代碼文件的大小,減小執行時間,但並不執行須要大量編譯時間的優化。在蘋果的編譯器中,在優化過程當中,嚴格別名,塊重排和塊間的調度都會被默認禁止掉。
  • Faster[-O2]: 編譯器執行全部不涉及時間空間交換的全部的支持的優化選項。在這種設置下,編譯器不會進行循環展開、函數內聯或寄存器重命名。和‘Fast[-O1]’項相比,此設置會增長編譯時間和生成代碼的性能。
  • Fastest[-O3]: 在開啓‘Fast[-O1]’項支持的全部優化項的同時,開啓函數內聯和寄存器重命名選項。這個設置有可能會致使二進制文件變大。
  • Fastest, Smallest[-Os]: 優化大小。這個設置開啓了‘Fast[-O1]’項中的全部不增長代碼大小的優化選項,並會進一步的執行能夠減少代碼大小的優化。
  • Fastest, Aggressive Optimizations[-Ofast]: 這個設置開啓了「Fastest[-O3]」中的全部優化選項,同時也開啓了可能會打破嚴格編譯標準的積極優化,但並不會影響運行良好的代碼。

一般Debug模式默認爲None[-O0],Release模式默認爲Fastest, Smallest[-Os],在實際的項目中,能夠根據項目的狀況設置優化級別。

須要注意的是,任何的優化都是基於時間和空間層面的考量速度的提高每每伴隨着空間開銷的增大,反之亦然

相關文章
相關標籤/搜索