Java: question about method invokation in for loops
I have a for loop with the following lines:
method1(similarities.instance(i));
method2(similarities.instance(i));
method3(similarities.instance(i));
If I store into a variable a = similarities.instance(i) and I change the for loop with:
method1(a);
method2(a);
method3(a)
;
do I get much better performances ? Because I'm not invoking the method at each iteration ? Is it something开发者_开发技巧 i should always do ?
I think you should store it, this will ensure no worth preformance, and also will not fail if similarites.instacnce()
has side effects you don't want to repeat
if similarities.instance(i) is equal to similarities.instance(i) for all invocation, your second approach is appropriate, and yes, you will get the better performance.
Updated(Based on Ingo's comment): And, in addition, the result must not be modified by any of the consumer methods
This depends mostly on what's inside similarities.instance()
. Below I assume that the 3 calls are perfectly identical.
If it's a plain simple getter, you most probably won't notice any performance difference either way. If it's resource-heavy, you may. Even then, though, I believe the JIT could easily optimize away the 3 distinct method calls, storing the result in a temporary. Bottom line is, you shouldn't worry about micro-optimization.
So in most of the cases, the only difference would be readability (which is important enough in itself). From this perspective, I would prefer option 2.
Like so many things it depends. If similarities.instance(i) is expensive to call then storing it in a variable and reusing it will make a difference in performance.
This is really not a question that can be answered in the general however. You need to profile/test the difference in performance to see if it makes a difference. I would say in most cases you shouldn't worry about it unless the method call is doing something slow like hitting a database or the network.
That said I would change it to the variable anyway to cut down on duplication.
This is safe to do if both of the following are true:
- similarities.instance() is a pure function
- method1, method2, method3 use their parameter read only.
By assigning that instance to a variable, you store in memory the physical address of that instance as a pointer.
By accessing every time that instance passing from the similarities object, you reach it passing each time through the memory address of the similarities
object, that somewhere and somehow stores in a memory structure the address of the instance you want to reach.
So yes, it definitely improves performances, although i'm not 100% sure if the JVM make some optimization itself. But i would say no.
"Premature optimization is the root of all evil" (c)Donald Knuth
Does this code really bootle neck? Did you do profiling or some tests for determining this?
In common case I see only one reason to store value of method invocation - better readability. Of cause value must be permanent and independent from invocation time. I mean this:
int a = someObj.getSomething();
method1( a );
method2( a );
method3( a );
This code is not good:
int length = someObj.getLength();
for( int i = 0; i < length; i++ )
{
...
}
And sometimes behavior could be opposite to your expectation.
精彩评论