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.