Pages: [1]   Go Down
Author Topic: whats faster?  (Read 1182 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Edison Member
*
Karma: 5
Posts: 1730
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Ayer, Massachusetts, USA
Offline Offline
Edison Member
*
Karma: 54
Posts: 1848
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

0
Offline Offline
Faraday Member
**
Karma: 24
Posts: 3488
20 LEDs are enough
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Check out my experiments http://blog.blinkenlight.net

Offline Offline
Edison Member
*
Karma: 5
Posts: 1730
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Offline Offline
Edison Member
*
Karma: 19
Posts: 1041
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Essex, UK
Offline Offline
Full Member
***
Karma: 4
Posts: 150
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: July 29, 2012, 03:58:11 pm by Andy Brown » Logged

Home of the Nokia QVGA TFT LCD hacks: http://andybrown.me.uk

UK
Offline Offline
Faraday Member
**
Karma: 99
Posts: 4153
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

Offline Offline
Edison Member
*
Karma: 5
Posts: 1730
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 129
Posts: 8535
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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
Logged

Rob Gray aka the GRAYnomad www.robgray.com

UK
Offline Offline
Faraday Member
**
Karma: 99
Posts: 4153
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

Offline Offline
Edison Member
*
Karma: 5
Posts: 1730
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 98
Posts: 4809
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.


Logged

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

0
Offline Offline
Shannon Member
****
Karma: 207
Posts: 12205
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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...
Logged

[ I won't respond to messages, use the forum please ]

Offline Offline
Edison Member
*
Karma: 5
Posts: 1730
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Pages: [1]   Go Up
Jump to: