Electronic – arduino – Can faster processors/clocks execute more code


I am writing a program to run on an ATmega 328 which runs at 16Mhz (its an Arduino Duemilanove if you know them, it's an AVR chip).

I have an interrupt process running every 100 microseconds. It's impossible, I would say, to work out how much "code" you can execute in one loop of 100 microseconds (I'm writing in C which presumably is converted to assembly then into a binary image?).

Also this would depend on the complexity of the code (a giant one liner might run slower than several short lines for example).

Is my understanding correct, in that my processor with a clock rate or 16Mhz performs 16 millions cycles per second (this means 16 cycles per microsecond 16,000,000/1,000/1,000); And so, if I want to do more in my 100 microsecond loop, buying a faster model like a 72Mhz version would give me 72 cycles per microsecond (72,000,000/1,000/1,000) ?

Currently it runs just a bit too slow, i.e. its taking a little longer than 100 microseconds to do the loop (how long exactly is too hard to say, but it gradual falls behind) and I would like it to do a little more, is this a sane approach getting a faster chip or have I gone mad?

Best Answer

In general the number of assembly instructions the device can execute per second will depend on the instruction mix and how many cycles each instruction type takes (CPI) to execute. You could in theory cycle count your code by looking at the disassembled asm file and looking the function you are concerned about, counting up all the different types of instructions in it, and looking up the cycle counts from the datasheet for your target processor.

The problem of determining the effective number of instructions per second is exacerbated in more complex processors by the fact that they are pipelined and and have caches and what not. This is not the case for a simple device like an ATMega328 which is a single instruction in flight processor.

As for practical matters, for a simple device like an AVR, my answer would be more or less "yes". Doubling your clock speed should half the execution time of any given function. For an AVR, however, they won't run faster than 20MHz, so you could only "overclock" your Arduino by another 4MHz.

This advice does not generalize to a processor which has more advanced features. Doubling the clock speed on your Intel processor will not in practice double the number of instructions it executes per second (because of branch mis-predictions, cache misses, and so forth).