Vector addition benchmark in C, C++ and Fortran
Posted on April 11, 2012 by Paul
If you were involved in working with large arrays and linear algebra, you’ve probably heard mantras like A Fortran implementation can usually achieve more performance than C or C++ or C++ is slow for scientific computations, don’t use it!. Until recently I honestly believed that, at least for elementary linear algebra operations, like vector addition, e.g. C can easily beat C++ (specifically that working with C-arrays should be faster than the more elaborate vector data structure from C++) and Fortran will beat both. Please be aware that I’m not talking about which language is inherently superior to another, but rather about what is the actual state of their implementation on personal computers running Linux and Windows.
In order to test the above myths I’ve implemented a few simple codes for vector, one dimensional arrays, addition in C, C++ and Fortran.
Adding two vectors is a simple operation you just need to pass through data only once and add element by element. A straightforward C implementation that adds 90 millions doubles is presented next:
The above code will simply report how much time it takes to add two 90 millions elements vectors on a given machine. If you have a C99 compatible compiler you can also use the restrict keyword:
On a modern core i7 processor with 16 GB or RAM the above code takes about 0.6 seconds with no optimizations and about 0.35 seconds with maximum compiler optimization. There was practically no speed difference between the original code and the one with the restrict keyword.
Let’s see the equivalent C++ implementation with STL vectors instead of C-arrays:
The code is shorter and, at least to me, it looks cleaner. What about the performance ? Well … this code takes about 1.23 seconds with zero optimizations! More than double the time of the C version. However if you enable full compiler optimizations this will take 0.23 seconds!
Both tests were done on the same machine and same operating system (Windows 7 64 bits) with the same compiler (Visual Studio 2010), each test was done ten times and the results were averaged. Maybe Microsoft has a crappy C compiler, after all they have no support for C99 …
Repeating the above two tests with gcc-4.7 and g++-4.7 on a Linux machine (64 bits Linux Mint) I’ve obtained 0.755 seconds and 0.345 seconds (with optimizations) for the C version; versus 1.1 seconds and 0.22 seconds (with optimizations) for C++. Can you see a pattern here ?
Apparently the compiler, for both GCC and Visul Studio, does a better optimization job for C++ than for C.
In true C++11 spirit we can further modify the add function:
This time the code is slightly slower on Windows with Visual Studio 1.29 seconds and 0.25 seconds (with optimizations) versus Linux and gcc where the code takes about 2.23 seconds and 0.22 seconds (with optimizations).
What about Fortran ? First, I’ve used a trial version of Intel Fortran for both Linux and Windows, from past experiences I knew Intel Fortran is one of the fastest Fortran compilers available for Intel processors.
Next is a straightforward Fortran implementation of vector addition:
If you are a modern Fortran priest you may notice that I’ve used an explicit loop for vector addition, I did this because, initially, I wanted to give a fair chance to C and C++. The above code scores, on Windows, about 0.66 seconds without compiler optimization which is way better than both C and C++ without optimizations. However, the optimized version takes about 0.29 seconds which is slightly worse than C++ but a much better score than C. The results on Linux are slightly worse.
In most modern Fortran books and tutorials you are advised to avoid the use of explicit loops for vector and array operations, the code is cleaner and the compiler can potentially use more optimizations for array operations. Having said that, we can replace the add subroutine with a single instruction:
The above code is actually a bit slower slower than the one with the explicit do loop, it takes about 0.89 seconds and 0.29 seconds (with optimizations).
The six codes used for this benchmark are available on GitHub together with an Excel file that contains the recorded result for each run.
If you are interested in learning more about the new C++11 syntax I would recommend reading The C++ Programming Language by Bjarne Stroustrup.
or, Professional C++ by M. Gregoire, N. A. Solter, S. J. Kleper 4th edition:
If you need to brush your Fortran knowledge a good book is Modern Fortran Explained by M. Metcalf, J. Reid and M. Cohen: