Alan wanted to check out just how much performance he could squeeze out of the ATMega328P. He needed was a way to measure the amount of time the CPU actually uses while executing various tasks. Using the Logic Sniffer to read a pin, that he toggles ON when the CPU is executing a task, and OFF when it is not. His testing is well documented, and he added some very good tips to speed up your code:
- Wherever possible, use the hardware facilities of the AVR to offload work and help keep your application responsive.
- Wherever possible, use interrupts to respond to external events. Also use them to handle output where there is a significant delay between each step of the output.
- As far as possible, move anything other than trivial processing out of your ISRs into tasks which are prioritised relative to the other processing in your application.
- Any kind of polling or spin-waiting is to be avoided as far as possible, the CPU cycles that such approaches consume can better be used for ‘real’ work.
- Avoid using the standard Arduino libraries, because they are focused on simplicity and ease of use rather than performance.
- Watch out for ‘hidden’ costs creeping into your code, such as the use of floating-point arithmetic when you don’t absolutely need to use it – examining the symbol table of your application is a good way of figuring out if you’ve inadvertently triggered its use.
- A better algorithm will always outperform micro-optimisation approaches such as hand-coded assembler – I’ll discuss this further in a later post where I describe how I implemented the RGB LED handling
Via the forum.