My programming ramblings

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:

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.

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.

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:

Disclaimer:All data and information provided on this site is for informational purposes only. solarianprogrammer.com makes no representations as to accuracy, completeness, currentness, suitability, or validity of any information on this site and will not be liable for any errors, omissions, or delays in this information or any losses, injuries, or damages arising from its display or use. All information is provided on an as-is basis. solarianprogrammer.com does not collect any personal information about its visitors except that which they provide voluntarily when leaving comments. This information will never be disclosed to any third party for any purpose. Some of the links contained within this site have my referral id, which provides me with a small commission for each sale. Thank you for understanding.