📝Fortran’s specialized for loop can generate faster code
- source
Fortran’s for
loops do not allow arbitrary modification of the counter variable. It also requires that iteration bounds are computed only once. That means that compiler can generate a code to pre-computes the number of iteration and then executes iteration check faster. (Decrement–compare to 0–jump can be performed with a single instruction on many processors.)
On the other hand, C’s for loop is very generic and it is required to execute the boundary check on every iteration. C compiler needs to do more analysis to perform the same optimization that is free in Fortran. That might be another reason why Fortran used to generate faster code than C.
See also: