Chapter 14. Optimization Techniques

Things should be made as simple as possible, but not any simpler.

—Albert Einstein

This chapter offers some tips to optimize code to reduce resource utilization. These techniques can be roughly divided into strategies for reducing memory usage, increasing code efficiency, and lowering power requirements. The need for low-cost versions of our products drives hardware designers to provide just barely enough memory and processing power to get the job done.

Most of the optimizations performed on code involve a tradeoff between execution speed and code size. Your program can be made either faster or smaller, but not both. In fact, an improvement in one of these areas can have a negative impact on the other. It is up to the programmer to decide which of these improvements is most important. Given that single piece of information, the compiler’s optimization phase can make the appropriate choice whenever a speed versus size tradeoff is encountered.

The first step in optimization is to determine which problems you have. You might have size issues, speed issues, or both. If you have one type of issue, you can have the compiler help you out with the optimization. If you have both size and speed issues, we recommend letting the compiler do what it can to reduce the size of your program. Then you can find the time-critical code or bottlenecks (where the program is spending most of its time) and manually optimize that code for speed. (In battery-powered ...

Get Programming Embedded Systems, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.