Fortran and C++ side by side
A numerical team chooses between these two languages every few years, and the argument is usually settled by
habit rather than by a measurement. This group takes one task per page, writes it twice, builds both with the
compilers in this image and runs both, and shows what came out. No page claims a number it did not measure: the
machine, the compiler versions, the flags and the number of runs are stated wherever there is a timing.
The comparison is meant to be fair to both. Fortran wins where arrays, a compiler that checks interfaces and a
syntax written for formulas matter; C++ wins where the program has to do everything else as well. Several pages
end with the same number in both columns, and that is the honest result rather than a disappointment: the
language is rarely what makes a numerical kernel fast.
What each page compares
| Question | Page |
|---|---|
| Is a whole-array expression shorter than a loop, and does it run as fast? | Arrays and matrix math |
| Which loop order does each language want, and how much does it cost? | Loops, layout and the cache |
| Do the two languages compute the same digits? | Floating point in both |
| What does a module check that a header does not? | Modules against headers |
| Does OpenMP behave the same from both languages? | OpenMP in both |
| How does one language call the other? | Calling across the boundary |
| Which one is faster on the same kernel? | The same kernel, timed |
| What does a change cost in each code base? | Maintainability and onboarding |
| What does each toolchain give for building and debugging? | Build tools and the debugger |
| So which one should the project use? | When Fortran is the right choice |
The pages assume the reader knows one of the languages and can read the other. A Fortran reader who has never written C++ will find the C++ side explained in the text where it differs from the obvious translation; a C++ reader will find the same in the other direction. The Fortran course is the place to learn the language itself.