Friday, May 16, 2008

Fortran faster than C++? I'm Not So Sure Anymore.

Okay, I admit I am biased about C++. (I also like python.) But, on an objective level, I wonder if Fortran is as fast as its users make it out to be.

From Fortran programmers I hear this frequent claim: Fortran is faster than C++ at numerical computing.

Where is the proof? Is there any truth to these claims?

Sure, a decade ago it was true. C++ was not standardized as a language until 1998, and compilers for C++ were terrible. But between standardization, and compiler improvements, I think the data now shows the above claim is wrong today.

Where is my evidence? Well, I haven't done a peer-reviewed study, but for starters I think the Computer Language Benchmarks Game is a pretty good indication of the current state of things. It's even recommended as a valid benchmark test by the Wikipedia. :)

The game tests each language using different compilers running several tests. Here are the results for the 10 fastest performers on Intel chips:

x-faster: Language: Mean score: -
1.0C++ Intel 1.233
1.1C++ GNU g++ 1.412
1.2C Intel 1.432
1.2C GNU gcc 1.442
1.3Lisaac 1.574
1.3D Digital Mars 1.602
1.3Pascal Free Pascal 1.642
1.4Clean 1.693
1.6Fortran Intel 2.015
1.7Haskell GHC 2.041

Take note: Both Intel and g++ compilers make faster code than both C and Fortran on any compiler! Go C++!

Next, a little more in depth comparison of C++ to Fortran on each different test.

Note how C++ is better in CPU time and memory usage in essentially every test. More info found here.

Now I admit, half the supercomputers out there still have older compilers and so you can't get these kind of results on them now. But still, as a rule of thumb, I would say C++ is faster than Fortran and C from the data I see here. Even if you use the g++ compiler!

This makes me think the above claim: C++ is slower than Fortran, was true when our thesis advisors were learning numerical computing, but is only being passed around today through ignorance.

However, I'd love to see some hard data to change my mind. (Please no data from 5+ years ago. My claim is C++ is better with modern compilers.)


  1. You can take note that many of the C++ programs are written using classes and object language.

    Therefore, I am not so sure object oriented programming hurts you in the speed and memory realm.

    These claims seem like tradition that gets passed down.

  2. I use to support some clusters in an academic environment and nearly 90% of the 'large' parallel codes were written in Fortran and the rest was C.


To add a link to text:
<a href="URL">Text</a>