Why performance matters in science
Scientific programs often run for hours or days, so a program that runs twice as fast can change what research is practical. Performance work in scientific computing concentrates on the few places where most of the time goes — usually loops over large arrays — rather than on polishing every line.
What compilers do
A compiler translates source code into machine instructions, and an optimizing compiler works hard to make that translation fast: it removes computations whose results are never used, replaces repeated calculations with cheaper equivalents, reorders and unrolls loops, and uses vector instructions that operate on several numbers at once. Optimization is typically selected with command-line flags that trade compile time against runtime speed. The compiler can only apply these transformations where the code's meaning is clear, so writing loops and array expressions in a straightforward style helps the optimizer do its work. More detail is in Optimization Concepts.
Memory and locality
Modern processors read from memory much more slowly than they compute, so programs that reuse nearby data — good locality — run far faster than programs that jump around. Fortran stores array elements column by column in memory, so loops that step through the first array index innermost touch memory in order and cache well. Simply matching loop order to the storage order is one of the most effective performance habits available.
Measure before you optimize
Guessing where a program is slow is unreliable. The dependable workflow is to time the program, find the routines that dominate the runtime, change only those, and compare against the saved timings. The measurement-first workflow article describes the loop in full.