编译优化 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],在实际的项目中,能够根据项目的状况设置优化级别。

须要注意的是,任何的优化都是基于时间和空间层面的考量速度的提高每每伴随着空间开销的增大,反之亦然

相关文章
相关标签/搜索