3

As I understand, in case of an array, JAVA checks the index against the size of the Array. So instead of using array[i] multiple times in a loop, it is better to declare a variable which stores the value of array[i], and use that variable multiple times.

My question is, if I have a class like this:

public class MyClass(){

    public MyClass(int value){
      this.value = value;
    }

    int value;
}

If I create an instance of this class somewhere else: (MyClass myobject = new MyClass(7)), and I have to use the objects value multiple times, is it okay to use myobject.value often or would it be better to declare a variable which stores that value and use that multiple times, or would it be the same?

2
  • 2
    Do whatever is the easiest to read. Commented Sep 2, 2012 at 21:22
  • 2
    This is exactly the kind of micro-optimization that you should not waste a second thinking about until you have a profiler telling you it's in a hot spot of your application. Commented Sep 2, 2012 at 21:42

3 Answers 3

4

In your case, it wouldn't make any difference, since referencing myobject.value is as fast and effective as referencing a new int variable.

Also, the JVM is usually able to optimize these kinds of things, and you shouldn't spend time worrying about it unless you have a highly performance critical piece of code. Just concentrate on writing clear, readable code.

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

Comments

2

The short answer is yes (in fact, in the array case, it does not only have to check the index limit but to calculate the actual memory position of the reference you are looking for -as in i=7, get the base position of the array and add 7 words-).

The long answer is that, unless you are really using that value a lot (and I mean a lot) and you are really constrained due to speed, it is not worth the added complexity of the code. Add to that that the local variable means that your JVM uses more memory, may hit a cache fault, and so on.

In general, you should worry more about the efficiency of your algorithm (the O(n)) and less about these tiny things.

2 Comments

Nonsense. There is no speed difference.
@nes1983 So the JVM takes care of these things completely. And there will be no speed difference even for the array[i] case.
2

The Java compiler is no bozo. He will do that optimization for you. There is 0 speed difference between all the options you give, usually.

I say 'usually' because whether or not accessing the original object or your local copy isn't always the same. If your array is globally visible, and another thread is accessing it, the two forms will yield different results, and the compiler cannot optimize one into the other. It is possible that something confuses the compiler into thinking there may be a problem, even though there isn't. Then it won't apply a legal optimization.

However, if you aren't doing funny stuff, the compiler will see what you're doing and optimize variable access for you. Really, that's what a compiler does. That's what it's for.

You need to optimize at least one level above that. This one isn't for you.

Comments

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.