A first simple optimisation

As a very simple but safe code-movement optimisation, I have made my stage move calls to Array.length where the Array in question is stored in a final local variable out of loops. This is safe because once an array has been created its length cannot change, even though its contents may. As the reference is in a final local variable we are also guaranteed that the same array is indeed being referred to each time around the loop.

I have just realised that this is still not quite safe, as there could be a final local variable declared inside the scope of the loop, in which case it would be re-initialised (possibly to a different value) each time through the loop.

For example:

int j = 0;
while (j < 5) {
final int[] innerArray = new int[j];
System.out.println("innerArray length " + innerArray.length);
++j;
}

Here the value of innerArray.length will change on each iteration of the loop, even though innerArray is final.

Advertisements

Tags: , ,

One Response to “A first simple optimisation”

  1. This time, maybe? « Code movement in Java programs with pure functions Says:

    […] Code movement in Java programs with pure functions A journal of my research for a BSc (Hons) project at VUW « A first simple optimisation […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s


%d bloggers like this: