whats faster?

I need to change one register often and another occasionally,either way I need to calculate values for both, but is it faster on average to set both regardless if there's a need for change in the one occasionally changed, or to check the register and only rewrite if necessary?

winner10920:
I need to change one register often and another occasionally,either way I need to calculate values for both, but is it faster on average to set both regardless if there's a need for change in the one occasionally changed, or to check the register and only rewrite if necessary?

I have not looked at the AVR instruction set, so I can't tell you based on specifics.

In general, if the second value is a small integer, and you only use basic integer operations (no divide, possibly no multiply), it is faster to always calculate the value than to do a test to determine if you should calculate it.

If you are doing floating point, it should be faster to skip updating the second value.

The real method is to code both, and time how many operations you can do in a particular time period.

That being said, a lot of the Arduino code that I've seen is not really time sensitive, as most is probably waiting for real devices, etc. There are times when you need to worry over every single cycle, and if you need to do it, you need to do it. If you are cpu bound rather than waiting for an event, then perhaps you should consider upgrading to a different processor that would be faster.

The proper approach to performance tuning is to first consider how fast the result has to be. "Fastest" is a pretty useless goal. Once you know how fast you have to be you implement it straight forward and ensure it is bug free. Then you measure how much better you have to become. Only if you are below the mark you need to think about optimizations. The point is that at this stage you will have an idea on how hard it will be.

I guess it doesn't have to be the fastest, either result will appear realtime, I guess the only way to know for sure it to test it, unfortunetly I only own a old oscilloscope so I can't time the actual response time,
is there a way to test it with the arduino itself? Or would the difference be so smal id have to use another piece of equiptment

winner10920:
I guess it doesn't have to be the fastest, either result will appear realtime, I guess the only way to know for sure it to test it, unfortunetly I only own a old oscilloscope so I can't time the actual response time,
is there a way to test it with the arduino itself? Or would the difference be so smal id have to use another piece of equiptment

You could look at the assembler code.
You could store the current time, run an empty loop for a million iterations, then subtract new current time from old time to figure out how long it took. Then repeat, but put your calculation inside the empty loop and check out the time difference.

The answer is, it depends...

If the register is in the 0..31 range then if you need to set/clear more than one bit at a time then it's faster to read it with "in", set/clear with an "and" / "or" then write back with an "out". That's one cycle per instruction, 3 cycles in all.

If you need to just set or clear one bit then "sbi" and "cbi" will do it in one 2-cycle instruction. Faster if you just need to do one bit.

Be sure to check out the timing notes in the Atmel datasheet, you may need to insert a 'nop' at strategic places to achieve synchronisation depending on what you're doing.

A word of caution...

I assume by "register" you mean an SFR (like PORTA, OCR0A, etc).

Some registers react to a write in hardware. Writing the same value repeatedly may cause strange things to happen.

I don't know the internals of the ATMEGA chips that well, but things like setting a timer period may well reset the timer counter to 0, or the duty cycle of a PWM output might well reset the phase counter to 0. Certainly on PIC microcontrollers this is quite common.

Hmm, so perhaps checking if its needed is a better approach for reliability?

or to check the register and only rewrite if necessary?

We really need to know more but in general if you are going to check, decide, maybe write you may as well just write in the first place if we are just talking about speed.

But as majenko said there are gotchas. Which registers are we talking about?


Rob

It depends on the register you are writing to. Check the datasheet for the chip to make sure.

Mostly OCR1A for timer 1, and then less often TCCR1A and TCCR1B to change the prescaler, I also set TCNT1 to zero when I change OCR1A
im wondering if I should
a) check if TCNT1 is <OCR1A to set it to zero or not
b) check if TCCR1B is set to the right prescaler

The situation doesn't require it to be the fastest possible more reliable really, but im interested in finding out which is the best way and if there are differences
I would already know if I needed to change OCR1A since that always changes, but the prescaler changes less often if not at all sometimes, so would comparing the current register to what it needs to be be a waste of time? Or just overwrite it everytime

Other than the ports this is my first time diving into the datasheet in depth and playing with the "sfr"s I guess they are called, so id like to do it the right way

Do a -- fetch and compare -- and maybe a write or just -- write --?

I think just write would be quicker even when a check would say it's not needed, the check is more.

OCR1A/B, TCNT1 etc can be written as often as you like, but the control registers like TCCR1A/B I would assume cause a timer reset when written unless experiment proves overwise...

Im not sure about if it resets, ill have to test it, I have it on fast pwm mode 15, so I can set the top and the fast pwm allows me to toggle uno pin 9 and 10 on opposite phases which I need
I wont know for sure until I hook up a quadrature encoder and see if it can keep up and respond smoothly