Frame of the question
I am currently editing an add-on module to an ocean/circulation model, which is written in Fortran. The code of the main model is quite optimized with respect to short run time (not with respect to readability of the code). There are some code parts in the add-on module that do not seem to be optimized - at least from my point of view.
At three occasion a temporary variable is introduced to substitute some calculations as follows:
! comment a2 = a * a b2 = b * b ! comment c = a / (a2 + b2) ! comment f = c * d
This seems reasonable to me if (1) I want to make the code better human-readable or (2) the
a * a is to be substituted several times in the code. However, (1) and (2) are not the case:
b2 are only needed once. In order to reduce the run time this formulation seems to be better:
f = d * a / (a * a + b * b)
The number of calculation steps remains the same. The declaration of three variables is removed. We loose some explanations in the code.
I think that in one previous version of the code
a * a was present several times which is why it was substituted by
a2. However, it was not removed.
- Is the a good reason with respect to "optimal" run time to keep the code as it is posted in the first code snipped?
- Does a state-of-the-art compiler (i.e. ifort) detect it and remove the unnecessary variables? Thus, both formulations do not make a difference?