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.

Cmp Arrays and matrix math The same five results from a vector and a 3 by 3 matrix: whole-array expressions against loops and the standard algorithms, and two outputs that diff to nothing. Open Cmp Loops, layout and the cache Column-major against row-major on 16 million doubles in both languages: 32 ms against 179 ms, and almost no difference between the two compilers. Open Cmp Floating point in both Single and double precision, the order of a sum, NaN and infinity, and the flags each compiler offers to break the rules. Open Cmp Modules against headers A module and its .mod file against a header and its declarations, the interface check that Fortran makes and C++ does not, and the link error that catches what is left. Open Cmp OpenMP in both The same parallel loop with -fopenmp in Fortran and in C++, the thread count, and the measured speedup on four logical CPUs. Open Cmp Calling across the boundary A C++ function called from Fortran and a Fortran subroutine called from C++, through iso_c_binding and extern "C", with the whole build sequence. Open Cmp The same kernel, timed A matrix multiply and a stencil in both languages, five runs each at -O2 and -O3, min and median, and what the numbers do and do not show. Open Cmp Maintainability and onboarding One requirement change applied to both programs: the lines it touches, what the compiler catches, and what a new reader has to know. Open Cmp Build tools and the debugger gfortran and g++ with make, a warning that only one of them gives, a gdb session in each, gcov on both, and the tools this image does not have. Open Cmp When Fortran is the right choice The closing page: the four cases where Fortran wins, the four where C++ does, and the mixed projects where the answer is both. Open

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.