When Fortran is the right choice
The pages of this group measured small things carefully. This one is the summary a reader can use: what the numbers do and do not say, the cases where each language is the better tool, and the shape of the project that uses both. It is an opinion, but it is an opinion built on the numbers on the pages before it, and each claim points at the page that supports it.
What this group measured
| question | result | page |
|---|---|---|
| Does the loop order matter? | 5.5 times, in both languages | Loops, layout and the cache |
| Does the language matter for the same loop? | about two per cent | Loops, layout and the cache |
| Does the library matter? | 4.6 times, MATMUL against the same loops | The same kernel, timed |
| Do the two languages compute the same digits? | yes, to the last bit | Floating point in both |
| Does OpenMP behave the same? | yes: 2 times on 4 logical CPUs in both | OpenMP in both |
| Which catches a stale interface? | Fortran, at compile time | Modules against headers |
| What does a change cost? | 25 lines against 10 for a median | Maintainability and onboarding |
| What does the toolchain give? | the same compiler, the same debugger, the same warnings | Build tools and the debugger |
Two conclusions follow from the table and they are both uncomfortable for a simple story. The language is not what makes a numerical kernel fast: layout and libraries are, and they are available in both. And the two languages are much closer than their reputations: the same digits, the same OpenMP speedup, the same debugger, within ten per cent on the same loops. What separates them is what surrounds the kernel.
When Fortran is the right choice
The program is arrays and formulas. A whole-array expression is the shortest way to write a numerical formula, and the intrinsic library (MATMUL, SUM, the reductions, the shape operators) is part of the language rather than a dependency. The array page shows a page of physics in eight lines; the C++ answer is longer and needs two headers and a loop.
The team is numerical. A group of physicists, engineers or statisticians who write code as a tool reads Fortran faster than C++, because the distance between the formula in the paper and the line in the file is smaller. That is a statement about the readers, not the language, and it decides more projects than any benchmark.
The interface must be checked. A module is a compiler-generated interface, so a caller cannot be out of date with the procedure it calls. In a code base with a hundred numerical routines and a dozen contributors, that check catches a class of bug that C++ catches at link time or not at all (the modules page).
The dependencies must be few. Fortran's standard library is small and stable, and a numerical core written in it builds with a compiler and nothing else for decades. A C++ project of the same age has accumulated libraries, and each is a thing to install.
When C++ is the right choice
The program does more than arithmetic. Files, networks, threads with shared state, a user interface, a plugin system, a database: C++ has libraries for all of it and Fortran has almost none. A program whose numerical core is ten per cent of its lines is a C++ program with a numerical part, and calling MATMUL does not change that.
The abstractions are the point. Generic code over types, containers, iterators, RAII and the standard library are C++'s strength, and a program that models a problem in types benefits from them. Fortran's derived types and generic interfaces are a smaller toolbox, as the object-oriented part of the course shows.
The team and the tools are C++. The hiring pool, the static analysers, the sanitizers, the package managers and the editors are all larger for C++. A project that cannot hire a Fortran programmer has its answer, whatever the benchmark says.
Performance must be tuned by hand. Cache blocking, SIMD intrinsics, thread pools and GPU offload are all more controllable from C++, and a kernel that needs the last twenty per cent is easier to write there. Fortran's DO CONCURRENT and OpenMP directives are a shorter route, not a longer one.
The mixed project
The answer for a large numerical system is usually both, with a boundary that is small and boring: a Fortran library of plain functions over double arrays, called from C++ through BIND(C) and extern "C", exactly as the interop page builds it. The C++ side owns the program, the files and the threads; the Fortran side owns the kernels and their tests; the boundary is a header and a module that both sides can read.
The costs of that shape are real and should be budgeted: two compilers in the build, two sets of flags for the checked build, a boundary that cannot pass derived types or allocatable arrays, and a debugging session that crosses the language line without telling you. They are smaller than the cost of writing a sparse solver in C++ or a GUI in Fortran, which is the trade most projects are actually making.
Beyond legacy
The worst reason to choose Fortran is that the code is already Fortran, and the worst reason to choose C++ is that Fortran is old. Both are arguments from inertia, and both lead to a project that nobody chose. The language should be chosen for the code that has to be written next: if it is arrays and formulas, Fortran is the shorter path; if it is the program around them, C++ is.
The pages of this group are also a warning against the benchmark argument in either direction. The same loop in the two languages differed by two per cent; the same loop in the wrong order differed by five hundred. A team that picks a language from a benchmark and writes the wrong loop has lost more than the choice could win.
The last word
Fortran is a small, strict language for numerical work, with arrays as values and a compiler that checks the interfaces between its units. C++ is a large language that can do numerical work as one of its many jobs, with a library ecosystem and a toolchain that no numerical language matches. Both compile to the same instructions on the same machine, and this group measured them doing it: the same digits, the same OpenMP speedup, ten per cent between the loops, and a factor of five between a loop and a library.
A reader who has come this far can write the kernel in Fortran, the program around it in C++, and the boundary in a header — or choose one and be right for the reasons on this page.
The measurements quoted on this page are the ones on the pages it links, taken with GNU Fortran (Alpine 15.2.0) 15.2.0 and G++ (Alpine 15.2.0) 15.2.0 on Alpine Linux 3.24.3 (x86_64) in a Docker container of 4 logical CPUs, on an Intel Core i3-6006U at 2.00 GHz with a 3 MB cache.
Article author: Arthur Isaev