[Udo Klein] took on the challenge of counting as fast as possible using an Arduino board. The quest involves a search for short-cuts that will let him drive a 20-bit counter as quickly as possible using the stock hardware. But the catch is that the Arduino environment has some overhead running in the background which will slow things down. He looks into each of these road bumps, then shows one way to get around them.
The code uses a command we don’t normally see in modern C embedded programming; the goto statement. He’s using this to bypass the extra cycles used by the Arduino’s in-built loop() function. The only command that is run within his hacked loop is a deeply nested set of macros. They’re toggling output pins using the hardware XOR built into the AVR chip. This is directly addressing the registers and thereby dumps the slowdown added by the digitalWrite() function.
The result is a software counter that toggles the output lights (see the clip after the break) at about 98.9% of the hardware clock speed. Pretty impressive, but [Udo] figures he can make it just a bit faster yet.