Adrien Gorrell
Adrien Gorrell

Reputation: 1319

Java optimization : local variable or function call

Which would you do ?

 doThings(folder.getInstructions()) ;
 for (Instruction instruction : folder.getInstructions()) {
    // do things
 }
functionCall(folder.getInstructions()) ;

Or this :

instructions = folder.getInstructions() ;
doThings(instructions)
for (Instruction instruction : instructions) {
  // do things
}
functionCall(instructions) ;

Above all, I would like to know when it is more efficient to store a value in a local variable, and when it is better to make function calls.

Upvotes: 10

Views: 3504

Answers (5)

rbhawsar
rbhawsar

Reputation: 813

I believe it depends upon the situation. if it requires to make the same method call many times(and you are getting the same value) and better make one call and store it into some local variable.

and if you need to call the function only once. no need to go for local variables.

Upvotes: 0

Rohit Jain
Rohit Jain

Reputation: 213311

Whenever, you want to use the result of a method more than once, its better to store the result of method invocation in some temp variable.. This saves processing time of method invocation.. This will not affect as much in this case.. But may affect when there are many invocation..

Also, remember, your local variables are stored on stack. So, having a temporary local variable is occupying space on stack. Although this is not much of a concern in small cases like this one.. But unneccessary local variables, should be avoided..

So, there are pros and cons of both methods..

Upvotes: 0

Tomasz Nurkiewicz
Tomasz Nurkiewicz

Reputation: 340803

More readable is more efficient. Temporary expressions and local variables need the same space and from CPU/JVM perspective it doesn't make much difference. JVM will do a better job optimizing/inling it.

However if getInstructions() method call is expensive, cache it in local variable. If it's just a plain getter, it will be inlined anyway. Also IMHO in your particular case local variable is more readable and maybe even more correct if getInstructions() may have different results over time.

Upvotes: 21

Russell Gutierrez
Russell Gutierrez

Reputation: 1385

My answer would be dependent on what type of application that I am trying to develop. For example,

The first code block is better if the values are changes rapidly during the application. This is good if you need accurate results.

The second one is good if you are sure that the values initially fetched would not affect the other parts of the code.

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1501626

That entirely depends on what getInstructions() does. If it's just returning the value of a field - and if you're confident that the field value won't change between calls - then you probably won't see any efficiency differences between the two snippets.

If, on the other hand, getInstructions() needs to make a dozen web requests, then clearly you want to avoid calling that several times.

Readability is more important than efficiency though. In this case I find the second option more readable anyway - it's clearer that you want to take three separate steps (two method calls and a loop) with the same value. On the other hand, I'm quite happy to write something like:

for (int i = 0; i < text.length(); i++) {
    ...
}

rather than breaking that out into a separate variable:

int length;
for (int i = 0; i < length; i++) {
    ...
}

It really depends on the context. Sometimes an extra variable helps, sometimes it doesn't, from a readability perspective. The efficiency perspective depends completely on what the method call is doing, and whether it's "inlinable" for the JIT.

Upvotes: 10

Related Questions