[An Introduction to GCC 学习笔记] 13 优化问题2

Loop Unrolling

A prime example of an optimization with a speed-space tradeoff is loop unrolling:

  • This form of optimization increases the speed of loops by eliminating the “end of loop” condition on each iteration.
  • Loop unrolling is an optimization that increases the speed of the resulting executable but also generally increase its size.
  • Loop unrolling is also possible when the upper bound of the loop is unknown, provide the start and end conditions are handled correctly.

Scheduling

  • The lowest level of optimization is scheduling, in which the compiler determines the best ordering of individual instructions.
  • Most CPUs allow one or more new instructions to start executing before others have finished. Many CPUs also support pipelining, where multiple instructions execute in parallel on the same CPU.
  • When scheduling is enabled, instructions must be arranged so that their results become available to later instructions at the right time, and to allow for maximum parallel execution.
  • Scheduling improves the speed of an executable without increasing its size, but requires additional memory and time in the compilation process itself (due to its complexity).

Optimization Levels

In order to control compilation-time and compiler memory usage, and the trade-offs between speed and space for the resulting executable, GCC provides a range of general optimization levels, as well as individual options for specific types of optimization.

  • An optimization level is chosen with the command line option ‘-OLEVEL’, where LEVEL is a number from 0 to 3.
  • It is important to remember that the benefit of optimization at the highest levels must be weighed against the cost. The cost of optimization includes greater complexity in debugging, and increased time and memory requirement during compilation.
  • For most purpose it is satisfactory to use ‘-O0’ for debugging, and ‘-O2’ for development and deployment.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值