Motherboard Forums


Reply
Thread Tools Display Modes

Efficiency in accessing array elements

 
 
Davide
Guest
Posts: n/a
 
      04-30-2004, 09:02 AM
Hello,
this could be a trivial for question for you, anyway:

I have an hi-level code operation in which I need to access several times
the same element of an array, say A(K).
Is it -in general- more efficient to assign, at the beginning of the
operation, that element to a local variable (X:=A(K))
to be used in the rest of the operation instead of using each time A(K)?

Example:

VAR_1 := A(3) + A(5);
VAR_2 := A(3) - A(2);
VAR_3 := A(2) - A(5);
VAR_4 := A(3) * A(2);
.....

is less efficient then:

A_2:= A(2);
A_3:= A(3)
A_5:= A(5)
VAR_1 := A_3 + A_5;
VAR_2 := A_3 - A_2;
VAR_3 := A_2 - A_5;
VAR_4 := A_3 * A_2;

?

Thank you.


 
Reply With Quote
 
 
 
 
Hans-Bernhard Broeker
Guest
Posts: n/a
 
      04-30-2004, 10:03 AM
Davide <> wrote:

> Is it -in general- more efficient to assign, at the beginning of the
> operation, that element to a local variable (X:=A(K))
> to be used in the rest of the operation instead of using each time A(K)?


Not quite _in general_, because any decent compiler will know better
than you do whether that's a good idea or not. If it doesn't, you
should consider throwing away the toy you have and getting a serious
tool instead.

If you don't trust my work, maybe you'll listen to Don E. Knuth
himself, who coined the phrase "Premature optimization is the root of
all evil."

I.e. don't bother with this kind of micro-optimization unless you're
*certain* that you need to, in a particular situation. Above all,
don't apply it as a general tactics --- you'll be wasting potential
readability and ease of maintenance for no gain, or possibly even a
loss in efficiency of the code.

--
Hans-Bernhard Broeker ()
Even if all the snow were burnt, ashes would remain.
 
Reply With Quote
 
Casey
Guest
Posts: n/a
 
      04-30-2004, 01:54 PM
Davide said...
> Hello,
> this could be a trivial for question for you, anyway:
>
> I have an hi-level code operation in which I need to access several times
> the same element of an array, say A(K).
> Is it -in general- more efficient to assign, at the beginning of the
> operation, that element to a local variable (X:=A(K))
> to be used in the rest of the operation instead of using each time A(K)?


Compile both methods, compare the assembly language output, and then
you can decide.

However, most likely you will see little difference if the compiler
does a decent job of optimization.



Casey
 
Reply With Quote
 
Thad Smith
Guest
Posts: n/a
 
      04-30-2004, 02:54 PM
Davide wrote:

> Is it -in general- more efficient to assign, at the beginning of the
> operation, that element to a local variable (X:=A(K))
> to be used in the rest of the operation instead of using each time A(K)?
>
> Example:
>
> VAR_1 := A(3) + A(5);
> VAR_2 := A(3) - A(2);
> VAR_3 := A(2) - A(5);
> VAR_4 := A(3) * A(2);


The two biggest variables that determine the answer are

1. Is the array local?
2. If not, what is the cost of accessing the array entry?

If local and indexed by a constant (as shown in the example), it
normally doesn't help to copy, since the constant-indexed element can be
accessed directly.

If local and indexed by a variable, the cost of accessing goes up, but
is still efficient in some architectures that allow you to use a
base+index addressing mode. In other architectures, such as typical
8-bit processors, it would involve several instructions to compute an
effective address, so there would be an earlier saving for temp
variables.

If the array is a parameter passed by reference, the cost of access goes
up again, since the compiler cannot adjust by a constant offset at
compile time and must have the base address in a register, rather than
in the instruction address field. If the index is a computed
expression, the cost potentially goes up, depending on whether the
compiler simplifies the expression or precomputes part of it.

In general, the higher the cost of indexed access, the more likely that
using temp variables will help.

To discover the tradeoff for particular compilers and subject code, look
at the output of the compiler.

Thad
 
Reply With Quote
 
Alan Balmer
Guest
Posts: n/a
 
      04-30-2004, 03:31 PM
On Fri, 30 Apr 2004 11:02:40 +0200, "Davide" <> wrote:

>Hello,
>this could be a trivial for question for you, anyway:
>
>I have an hi-level code operation in which I need to access several times
>the same element of an array, say A(K).
>Is it -in general- more efficient to assign, at the beginning of the
>operation, that element to a local variable (X:=A(K))
>to be used in the rest of the operation instead of using each time A(K)?
>

Probably not - the compiler will do that itself if appropriate.
However, it may (and I emphasize *may*) be more understandable and
easier to maintain, if that particular array element has a
well-defined meaning of its own, aside from being just an element of
the array.

In general, go for readability and maintainability and let the
compiler worry about the details.

--
Al Balmer
Balmer Consulting

 
Reply With Quote
 
Grant Edwards
Guest
Posts: n/a
 
      04-30-2004, 03:32 PM
On 2004-04-30, Davide <> wrote:

> Is it -in general- more efficient to assign, at the beginning
> of the operation, that element to a local variable (X:=A(K))
> to be used in the rest of the operation instead of using each
> time A(K)?


If you've got a decent compiler, there will be no difference:
the compiler will figure out what values get used most and keep
those values in CPU registers. OTOH, there are some crappy
compilers out there, so it may help to go the local variable
route. The only way to know is to try it and see. In reality
it probably just doesn't matter.

-In general-, write the function in the way that makes the code
easiest to read and maintain. Worry about efficiency of the
function _after_ you've got hard data showing you that your
system performance isn't good enough _and_ it's because of that
function.

--
Grant Edwards grante Yow! I'm a nuclear
at submarine under the
visi.com polar ice cap and I need
a Kleenex!
 
Reply With Quote
 
 
 
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
RAID 0+1 array disaster. Asus A8V Deluxe. Please someone help. kelendral Asus 1 04-06-2008 11:31 PM
GA-K8N Ultra-9 and Raid-5 problem Getsome Gigabyte 3 07-23-2007 05:06 AM
New Intel Array Manager for XPS 410/Dimension 9200 Tom Scales Dell 9 06-27-2007 05:46 PM
Lost stiped RAID array after bios upgrade - Crosshair Greybeard Asus 7 04-24-2007 02:23 AM
Rebuilding RAID Array On P4C800 milkmandan Asus 0 03-20-2007 10:28 PM


All times are GMT. The time now is 05:31 PM.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44