3
for (int i = 0; i < array.length; ++i) {
    // Do something referencing array[i]
    // Do another thing referencing array[i]
    ...
}

In code such as this, is it actually useful to set a variable like currentValue = array[i] and then reference that instead of array[i]? I feel like the compiler would be smart enough to do something like that and render such code pointless.

5
  • 1
    what is the advantage of converting creating another copy of reference ? Commented Jul 15, 2013 at 21:16
  • JIT needs that loop to be looped by another loop to be kicked into optimization. Commented Jul 15, 2013 at 21:17
  • 2
    As a general rule, trust that the compiler is smarter than you until you have hard numbers showing otherwise. Commented Jul 15, 2013 at 21:17
  • 4
    Strive for readability and clarity. Such micro-optimizations won't have any noticeable effect, if any. So if you find it clearer to introduce a local variable, do it. If you find it clearer not to introduce one, don't. Commented Jul 15, 2013 at 21:18
  • In fact, in many such cases "the rules" will prevent the compiler (or JITC) from "commoning" such accesses. And in very many cases using a temp will be clearer than repeating the access, since it's easier to see that the same value is being used multiple places. Commented Jul 15, 2013 at 21:32

1 Answer 1

4

If you read the byte code that the compiler generates you will see that it does no such optimization. This means that in interpreted mode the array lookup will be done every time. If the method with the loop runs enough many times the JIT compiler will have another look at it and may optimize it.

Conclusion: if you want predictable results, store the array element in a local variable. More importantly, that way the code becomes more readable too.

Sign up to request clarification or add additional context in comments.

4 Comments

Have you measured that storing it in a private variable, even non-optimized, is predictably faster?
@JBNizet - It's worthwhile to note that not only is the second array access eliminated, but also the array bounds check for that access. And in an optimizing JITC, if "the rules" prohibited the JITC from commoning the access, the second access is a second potential point of exception which "breaks" other optimizations.
I agree with that, and it's probably faster, but without a measurement, I wouldn't blindly assume it's faster. And anyway, that will probably have a negligible impact compared to optimizing IO, DB requests, etc. I also agree that it's often more redable to introduce a local variable.
@JBNizet, executing all those iload and aload opcodes must amount to something, no matter how small, but I agree that real-life performance can be difficult to predict.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.