A = Z(X) + W(X) ! all identifiers are non-pointer, non-allocatable, scalar, default real
B = Z(X) ! all functions are EXTERNAL, all interfaces are implicit
A = B + W(X) ! B is never mentioned anywhere else and is not associated with anything
Which one might execute faster and why?
- The first variant
- The second variant
The first.
Fortran rules for function references in an expression within a statement state the evaluation of a function reference shall neither affect nor be affected by the evaluation of any other entity within the statement. This allows the compiler to assume that neither Z nor W can change the value of not only X but anything that would affect the function results. It can then, safely, schedule Z and W on separate execution resources and perform the calculation concurrently. No such licence is available for the second form because the compiler cannot be certain that Z does not modify X (or anything else that might affect computation of W), so it has to schedule Z and W execution sequentially.
This is sometimes interpreted as Fortran functions are pure functions and you can see why this is too strong a statement to be true (after all, Fortran has PURE/IMPURE keywords). But in the limited context of functions references in a statement, it kind of is. The compiler never saw any PURE or SIMPLE attribute and yet was able to make the inference. Fortran subroutines are not just “functions without a return value”.
For the purposes of the rule just described, the IF/WHERE/FORALL statements have special handling (they are treated as the equivalent construct).
A separate issue: Will statements in Z and W be executed?
- Yes
- Maybe
Maybe.
There’s a wonderful sentence in Fortran semantics: It is not necessary for a processor to evaluate all of the operands of an expression, or to evaluate entirely each operand, if the value of the expression can be determined otherwise.
“Otherwise” includes magic, undiscovered tech or maths, divine inspiration, mind-reading, time travel. All of those things could improve a compiler. If the compiler detects that you are trying to calculate and print the gazillionth hexadecimal digit of pi, it could just look it up, or use a better calculation than the one you provided.
Please correct me if I have misunderstood anything.