Chapter 10. Faster Code

I mentioned at the very beginning of Chapter 1 that C was designed for machines with limited resources—at least by today’s standards. Microcontrollers have many of those same limitations, making C a pretty natural fit as a development language. Indeed, if you want to get the maximum possible performance out of a tiny chip, C’s ability to work directly with memory addresses, as we saw in “Addresses in C”, is unparalleled, if tedious.1

I’m happy to say that even without diving into the depths of datasheets (seriously technical specifications produced by component and microcontroller manufacturers), you can employ several straightforward tricks to speed up your code. But do remember that sometimes good enough is, well, good enough! Try getting your code going with the patterns you know first. Does your program run? Does it do what you need? If so, the interesting options I highlight in this chapter, such as using integers rather than floating point numbers or unrolling loops, are just that: interesting. They are not really “better” and certainly not necessary. Usually. The Arduino and its cousins are definitely more limited than desktops. Sometimes your program won’t run or doesn’t quite do what you need. In those instances, consider bringing some of the following optimizations to bear.

The Setup

Rather than toss another slew of new gadgets and configurations and wiring diagrams at you, I’m going to focus on a hardware setup similar ...

Get Smaller C 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.