Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing an array element directly vs. assigning it to a variable

Performance-wise, is it better to access an array element 'directly' multiple times, or assign its value to a variable and use that variable? Assuming I'll be referencing the value several times in the following code.

The reasoning behind this question is that, accessing an array element presumably involves some computing cost each time it is done, without requiring extra space. On the other hand, storing the value in a variable eliminates this access-cost, but takes up extra space.

// use a variable to store the value
Temp = ArrayOfValues(0)
If Temp > 100 Or Temp < 50 Then
    Dim Blah = Temp
    ...

// reference the array element 'directly'
If ArrayOfValues(0) > 100 Or ArrayOfValues(0) < 50 Then
    Dim Blah = ArrayOfValues(0)
    ...

I know this is a trivial example, but assuming we're talking about a larger scale in actual use (where the value will be referenced many times) at what point is the tradeoff between space and computing time worth considering (if at all)?

like image 313
parkker007 Avatar asked Aug 19 '11 22:08

parkker007


2 Answers

This is tagged language agnostic, but I don't really believe that it is. This post answers the C and C++ version of the question.

An optimizing compiler can take care of "naked" array accesses; in C or C++ there's no reason to think that the compiler wouldn't remember the value of a memory location if no functions were called in between. E.g.

int a = myarray[19];
int b = myarray[19] * 5;
int c = myarray[19] / 2;
int d = myarray[19] + 3;

However, if myarray is not just defined as int[] but is actually something "fancy", especially some user defined container type with a function operator[]() defined in another translation unit, then that function must be called each time the value is requested (since the function is returning the data at location in memory and the local function doesn't know that the result of the function is intended to be constant).

Even with 'naked' arrays though, if you access the same thing multiple times around function calls, the compiler similarly must assume that the value has been changed (even if it can remember the address itself). E.g.

int a = myarray[19];
NiftyFunction();
int b = myarray[19] * 8;

There's no way that the compiler can know that myarray[19] will have the same value before and after the function call.

So- generally speaking, if you know that a value is constant through the local scope, "cache" it in a local variable. You can program defensively and use assertions to validate this condition you've put on things:

int a = myarray[19];
NiftyFunction();
assert(myarray[19] == a);
int b = a * 8;

A final benefit is that it's much easier to inspect the values in a debugger if they're not buried in an array somewhere.

like image 136
dash-tom-bang Avatar answered Oct 05 '22 16:10

dash-tom-bang


The overhead in memory consumption is very limited because for reference types it's just a pointer (couple of bytes) and most value types also require just a few bytes.

Arrays are very efficient structures in most languages. Getting to an index doesn't involve any lookup but just some math (each array slot takes 4 bytes so the 11th slot is at offset 40). Then there is probably a bit of overhead for bounds checking. Allocating the memory for a new local var and freeing it requires a bit of cpu cycles as well. So in the end it also depends how many array lookups you eliminate by copying to a local var.

Fact is that you really need exceptionally crappy hardware or have really big loops for this to be really important and if it is run a decent test on it. I personally choose often for the seperate variable as I find that it makes the code more readible.

Your example is odd btw since you do 2 array lookups before you create the local var :) This makes more sense (elimination 2 more lookups)

Dim blah = ArrayOfValues(0)
if blah > 100 or blah < 50 then
...
like image 37
Eddy Avatar answered Oct 05 '22 16:10

Eddy