1

i want to write my java programm as performant as i can so i'm reducing everything to have best performance... In the programm a method will called apprx. 9million times. there i have to calculate somthing, just subtracting two integers and these are needed twice. so my question is, what is faster: initialize new integer with the result of calculation or just calculate the values twice? e.g:

int result = a-b;
methodToCall(result, foo, bla);
otherMethod(result, bla, foo);

or

methodToCall(a-b, foo, bla);
otherMethod(a-b, bla foo);

i cant see a difference directly, but sometimes its with the first method a little bit faster... in general: is the first method always better? e.g when using other types of calculation (more complex). Is the java compiler or the jvm doing something with it to optimize it, e.g. see that i do the same calculation twice and doing it only once and cache the result by its own?

2
  • 3
    Whenever it comes to performance, I usually like clear results (although explanations are nice as well), so I benchmark things. I suggest you benchmark the start and end time of calling the methods for maybe 5000 times in both ways. Compare the results. Then do this for 10000. By then your answer should be clear. That's how I would do it. Commented May 5, 2011 at 8:25
  • Measure. Then decide. My hunch:Too close to call. The difference will be negligible, especially as the JVM is pretty clever with optimisations. Commented May 5, 2011 at 8:48

3 Answers 3

4

The first will theoretically be faster.

In the second, the JVM will not only calculate a-b twice, but will also temporarily assign storage for the result twice before passing it to the two method calls.

I just ran tests for these cases 100 million times and came up with just 10-15ms difference between them with the first being faster. My test results will be skewed because a and b are constants, but it nevertheless seems to confirm the theory.

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

3 Comments

the method could also be called 900million times, so the difference is "feelable" :) thank you!
Of course, check your algorithm - method calls etc. They will be far more expensive than this assignment/calculation of int values.
yes of course, but i reduced it bare to the bone :) i have now a nice runtime!
3

Simply measure the time. Without knowing the facts you can't do any conclusions. See this question about the tools.

If the difference is hard to notice, always prefer the well readable code.

Comments

1

The JVM may optimise the code in the second case to act like the first. However if it doesn't it may cost you 1 clock cycle which might be about 3 ms (if you call it 9 million times on a 3 GHz processor)

I prefer the first form because it reduces repetition which for me makes it easier to read. If your expression is more expensive, it could make more of a difference.

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.