|
|
| Author |
Message |
mikep
Joined: 24 Sep 2005
Posts: 771
Location: Austin, TX
|
|
Posted: 11 January 2009, 18:57 PM Post subject: |
|
|
It all depends where it is done.
They are the same if done in Main because the compiler can pre-compute the address of an array element because the address of the array is known at compile time.
If the array is a parameter to a function or subroutine then the compiler has to use "indexed" addressing where it loads (or saves) data based on an index from an address.
If you write the code using a variable for the index then this is also indexed addressing where the compiler has to first load the start address of the array and then "index" down to the specific element. |
|
| Back to top |
|
 |
sturgessb
Joined: 25 Apr 2008
Posts: 246
Location: Norwich, UK
|
|
Posted: 11 January 2009, 21:17 PM Post subject: |
|
|
Thanks Mike
So am i right in thinking then that
arrayvariable(1) is the same speed as a standard single var, but arrayvariable(variable) would be slightly slower?
Ben |
|
| Back to top |
|
 |
mikep
Joined: 24 Sep 2005
Posts: 771
Location: Austin, TX
|
|
Posted: 11 January 2009, 22:36 PM Post subject: |
|
|
| sturgessb wrote: |
So am i right in thinking then that
arrayvariable(1) is the same speed as a standard single var, but arrayvariable(variable) would be slightly slower? |
Yes assuming that arrayvariable is defined as global data outside of the ISR.
In terms of native code, indexed addressing usually generates one or two extra AVR machine instructions to setup the index register (usually Y or Z). - say 4 clock cycles.
If you asking about whether you should optimize the ISR by loop unrolling or some other method, I would suggest you measure the performance first and only optimize if necessary. If you plan to try optimizing this way I would go straight to AVR assembler language and enclose the code using #asm and #endasm. |
|
| Back to top |
|
 |
|