這是我參與更文挑戰的第2天,活動詳情查看: 更文挑戰前端
編譯連接自己是一個很大的體系內容,咱們後續再開文章專門詳細說明;後端
咱們都知道OC是一門編譯型語言,Xcode內置的編譯器已經從GCC
轉換爲LLVM
,LLVM
又分爲前端Clang
和後端LLVM
,爲了提升編譯後的機器碼執行效率,編譯器會在編譯過程當中把一些編譯期就能夠肯定的代碼進行優化,從而提升執行效率;markdown
例如app
int a = 5;
int b = 3;
int c = a + b;
複製代碼
這幾行代碼在通過優化後可能就會合併成一句代碼了,從彙編層面能夠很清楚的看到未優化前指令如圖所示, ide
當設置優化後的指令如圖所示函數
能夠看到:oop
add
運算後再寫回x0寄存器進行使用;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.優化
一般Debug模式默認爲None[-O0],Release模式默認爲Fastest, Smallest[-Os],在實際的項目中,能夠根據項目的狀況設置優化級別。
須要注意的是,任何的優化都是基於時間和空間層面的考量,速度的提高每每伴隨着空間開銷的增大,反之亦然;