Go Down

Topic: How fast can you count in software (Read 2715 times) previous topic - next topic

udoklein

May 01, 2012, 08:40 am Last Edit: Jun 03, 2012, 05:05 pm by Udo Klein Reason: 1
Every once in a while I see the question how fast an Arduino might be able to toggle some pin. This question is trivial if you allow PWM (8 MHz) or even re-fusing (16 MHz). So I started to wonder how fast I could do this in software. Then I wondered how fast I could count in software. Obviously a somewhat more interesting challenge. So here is my first take on creating a 20 bit counter with an Arduino in software: http://blog.blinkenlight.net/experiments/counting/fast-counter/. A small exercise in agressive performance tuning ;)
Check out my experiments http://blog.blinkenlight.net

Chagrin

Very nice. I was watching the video wondering why I never see it go to zero then... oh yeah. ;)

I know what you mean by seeing people asking the "how fast" question, and it frequently manifests itself with a later suggestion of using, for example, interrupts when watching input or PWM for fans or heaters when it's not necessary. And although you certainly demonstrated how quickly pins can be toggled I think you kinda missed answering the hypothetical question; it would be nice seeing an example using code that a normal mortal would use.

udoklein

Which hyptothetical question? Toggling just one pin in software - this is something that I just would not do. PWM is the way to go.
Check out my experiments http://blog.blinkenlight.net

udoklein

Take two on how fast I can count in software http://blog.blinkenlight.net/experiments/counting/faster-counter/. The older Version reached 98.9% of the theoretical limit. The new version reaches 99.9% of the theoretical speed limit. So depending on how you look at it the new version is 1% better or ~10 times better ;)

It nicely shows that it will usually not pay off to squeeze the last tiny bit of performance out of a system. It also shows how to agressively optimize.

I know that there is still some tiny little bit of performance that could still be squeezed out of the code. Basically what I would do is to prepare the stack and replace the subroutine calls with jumps / gotos. Thus I would decrease the call overhead. Any suggestions for further improvements? Not that there is any point in pushing this so far but to see how far it can be pushed :)
Check out my experiments http://blog.blinkenlight.net

MikeDeKlerk

Quote
Every once in a while I see the question how fast an Arduino might be able to toggle some pin


Answer:
Quote
The frequency of the PWM signal is approximately 490 Hz.


Source:
http://arduino.cc/en/Reference/analogWrite

udoklein

#5
Jun 03, 2012, 05:05 pm Last Edit: Jun 03, 2012, 05:26 pm by Udo Klein Reason: 1
As I already mentioned
Quote
This question is trivial if you allow PWM (8 MHz) or even refusing (16 MHz).
Analog write does not give you maximum toggle speed at all.
Check out my experiments http://blog.blinkenlight.net

kf2qd

PWN is a Duty Cycle output - Meaning it is On for some portion of the time based on a ratio of N/256. It is fixed frequency and thus fixed speed.

If you want to be able to do some function with a 328 or any other processor you need to look into assembly language. You can eliminate teh transfers to RAM by keeping everything in registers (its still RAM, but its faster code...) minimal use of subroutine calls and minimal pushes and pops. The fewer instructions the faster.

udoklein

Quote
PWN is a Duty Cycle output - Meaning it is On for some portion of the time based on a ratio of N/256. It is fixed frequency and thus fixed speed.


Sorry, but this is just wrong. Read the datasheet if you do not believe me.

Quote

If you want to be able to do some function with a 328 or any other processor you need to look into assembly language. You can eliminate teh transfers to RAM by keeping everything in registers (its still RAM, but its faster code...) minimal use of subroutine calls and minimal pushes and pops. The fewer instructions the faster.


Your advice tells me that you did not read my article. Not only that I know that keeping everything in registers makes it fasters. I actually describe in great detail how to do it.
Check out my experiments http://blog.blinkenlight.net

Go Up