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?