I am currently working on code that will have hundreds of thousands of iterations and want to know if modern Java compilers automatically handle intermediate values during optimization into assembly. For instance, I have the following code in the loop (simplified):
arrayA[i] += doubleA*doubleB;
arrayB[i] += doubleA*doubleB;
Is a modern Java compiler 'intelligent' enough to store doubleA*doubleB into a multiplication register (and then proceed to read from the multiplication register for the second array, avoiding a second floating point operation)? Or, would I be better off with the following:
double product = doubleA*doubleB;
arrayA[i] += product;
arrayB[i] += product;
For the second option, I would primarily be concerned about the overhead of Java's garbage collector dealing with the product variable every single time it goes out of scope.