5

I recently read a StackOverflow question that indicated, when accessing variables, it is faster to use the stack than the heap:

void f() {
    int x = 123; // <- located in stack
}

int x; // <- located in heap
void f() {
    x = 123  
}

However, I can't work it through my head which is faster in my example (since I assume they are both using the stack). I'm working on hitbox calculation and such, which uses alot of X-Y, width, height variables (up to 10-20 times for each) in the function.

Is it faster to use an object's get() method each time or set it to a local variable at the start of the function?

In code, is it faster to (or more efficient to):

void f() {
    doSomething(foo.getValue() + bar.getValue()); 
    doSomethingElse(foo.getValue(), bar.getValue());
    doAnotherThing(foo.getValue(), bar.getValue());
    // ... <- lot's of accessing (something.getValue());
}

or

void g() {
    int firstInt = foo.getValue();
    int secondInt = bar.getValue();

    doSomething(firstInt + secondInt);
    doSomethingElse(firstInt, secondInt);
    doAnotherThing(firstInt, secondInt);
    // ... <- lot's of accessing firstInt and secondInt
}

when foo and bar are MyObject's

public class MyObject {
    int x = 1;
    public int getValue() {
        return x;
    }
}

If they are about the same efficiency, how many times do I have to perform a .getValue() for it to become less efficient?

Thanks in advance!

8
  • 1
    Please don't try and optimize your code like this. First make it work... in general, these micro-optimizations are meaningless. You need to benchmark changes in real code to determine if they matter for your use case. Commented Dec 23, 2013 at 19:37
  • 4
    Why does it make a difference? Please google search "premature optimization". Commented Dec 23, 2013 at 19:37
  • 1
    If you want to know where the turning point is for efficiency, you'll need to conduct a proper microbenchmark yourself. The answer varies with your hardware and your software. Commented Dec 23, 2013 at 19:40
  • I'm sure I'm not the only one that was unaware of the existence of benchmarking. My code does work, so I was simply asking if it does make a difference, and if anyone knew, what the difference was. Commented Dec 23, 2013 at 19:50
  • 1
    I wouldn't trust the post you link. Measure properly and you will most certainly find no difference... Commented Dec 23, 2013 at 20:01

2 Answers 2

10

JIT will change (optimize) your code on runtime, so this is not important in Java. One simple JIT optimization is method inlining.

For further optimization read about Micro Benchmarking and look at this question How do I write a correct micro-benchmark in Java?

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

Comments

-1

If you do use local variables, you may tell the compiler that the value will not be changed final int x = ...;, turning it in some kind of local constant.

Reassigning values (recycling objects and such) may help reduce garbage collection (GC).

Write some extreme stress test, if possible a visual one too, let it run for a long time and measure real performance, perceived performance. A better faster time is not always better, sometimes it may be a bit slower but a bit smoother across the execution.

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.