Uma Kanth
Uma Kanth

Reputation: 5629

Why is getting a value from a variable preferred than getting it from an array?

Consider the below program:

int []array = {10, 20, 30, 40, 50};
int x = array[0];
for(int i = 0; i < array.length; i++)
{
 System.out.println(x * array[i]);
 System.out.println(array[0] * array[i]);
}

When an array is declared, it is stored in the memory which reside together.

array:     [0]    [1]    [2]    [3]   [4]
Value:     10     20     30     40    50
Location:  100   102     104    106   108

Now when x is assigned a value which is the first element of the array(10).

x is stored a value in the memory with value 10.

variable :  x
Value :     10
Location :  120

Now when we call the for loop,

The first print statement will refer to the value at location 120 and say that I have a value 10.

The second print statement will also refer to a location 100 and say it has a value 10.

When both the statements have to look for the memory location every time, why would the first statement be more optimal?

EDIT In the same program if x is used multiple times.

int []array = {10, 20, 30, 40, 50};
int x = array[0];
for(int i = 0; i < array.length; i++)
{
 System.out.println(x * array[i] + (x + 1) * array[i] - ( x + 2));
 System.out.println(array[0] * array[i] + (array[0] + 1) * array[i] - (array[0] + 2));
}

Here x is used many times. So people prefer using x than calling array[0] everytime.

Both have to be looked up though.

Upvotes: 0

Views: 129

Answers (1)

Raniz
Raniz

Reputation: 11113

When accessing array[i], the processor has to look up the memory address for array and then add i to that address to get to the variable you requested.

When accessing x the processor can get the variable directly from x.

In Java, only primitives are stored on the stack so array will be a pointer to something on the heap whereas x will be stored on the stack. This only applies if x is a primitive though. If array contains objects (and x is thus a pointer to an object), both will be stored on the heap.

So the main difference is that when accessing array[i] you'll be accessing location-of-array-plus-i, but when accessing x you'll be accessing location-of-x. It's worth noting that array[0] (or any other fixed index) can usually be optimised into a single instruction.

Now, should you worry about this? No, absolutely not. There is unlikely to even be a measurable performance difference between these two ways and even if there is it is extremely unlikely that the performance difference will affect you - your application is probably doing a lot more time-consuming work than just accessing variables in memory. The compiler will also very likely optimise this to reduce the number of memory lookups required and the processor caches will play a role too.

The real reason it is preferable to write something like:

int x = array[0]
int y = array[1]
int result = x + y + x * y + x * (x - y)

Is that it is a lot more readable than:

int result = array[0] + array[1] + array[0] * array[1] + array[0] * (array[0] - array[1])

Upvotes: 3

Related Questions