Thoughts on Programming Languages for Scientific Computing

7 #

I have a friend who works in chemistry / protein folding and thus does a lot of computational stuff within that field. Coming from an application/web programming background, I was utterly shocked when she mentioned that FORTRAN is widely used in computational simulations. The great programmer Nietzsche once declared that “FORTRAN is dead”, and I held that belief too, thinking that FORTRAN was only used in the days when C and C++ weren’t developed and programmers didn’t have anything else. I didn’t know a single person who uses FORTRAN.

But I was wrong. It turns out that FORTRAN is still actively used in scientific computing. In addition, it is not an “outdated language”. FORTRAN actually has quite a few versions which have evolved and added on modern programming languages features over the years.

The biggest draw of FORTRAN is speed. It’s one of the lowest level out of all the high level programming languages (like C, C++, Pascal, Java, etc.). The language, intended for scientific computing, was designed to be fast. In comparison, languages like C and C++ were designed as general purpose languages that could be used for a variety of purposes.

But I wasn’t totally sold on FORTRAN. I don’t like the syntax and rather code in C++ any day. I didn’t believe that C++ was all that much slower than FORTRAN so I decided to do some research into this matter. The following comes from an email I wrote:

  1. FORTRAN is widely cited to be “20% to a factor of ten” faster than C++. I didn’t find anything for C, but I think the speed of C is about the same as C++.
  2. However, the data in #1 was published in 1997. In the modern day, C and C++ compilers have been heavily optimized to produce speeds comparable to FORTRAN. Some modern benchmark results are here:

    (These two benchmarks show FORTRAN to be about the same as C/C++
    depending on how the code is written and compiled)
    http://dan.corlan.net/bench.html (maybe around the year 2000)
    http://dan.corlan.net/amd64_dual_core_benchmarks.html (newer version)

    (This benchmark shows FORTRAN to be much slower than C/C++. Java is
    even faster!)
    http://shootout.alioth.debian.org/gp4/benchmark.php?test=all&lang=all

    (This benchmark shows C++ being slightly faster than FORTRAN)
    http://pauloherrera.blogspot.com/2006/12/introduction-ive-read-lot-of-things.html

  3. In addition, there is a library for C++, called Blitz++, which allows C++ programs using the library to approach the speed of FORTRAN. Benchmarks are here:
    http://www.oonumerics.org/blitz/benchmarks/
    (Note the percentages, ie. 95.7%, is the speed relative to the
    comparable FORTRAN program. So 100% is the same speed.)

    Also note that the benchmarks for Blitz++ were completed in like the year 2000. I expect if the same benchmarks were run today with modern compilers, the results would be even better.

  4. Therefore, I conclude that C/C++ is comparable to the speed of FORTRAN and depending on what you are doing and how you write the code can be faster than FORTRAN. So it depends on your preference of the programming language. I would probably always pick C++ over FORTRAN since I am more familiar with C++ and I think C++ code is easier to write and read.
  5. Surprisingly, python *can* be really fast. See:
    http://scipy.org/PerformancePython
    Of course, it will never approach the speed of C++ or FORTRAN, but Python code can be very easy to write and for medium sized projects, you can write the code much faster.
  6. Surprisingly, Java *can* be faster than C/C++. This is largely attributed to compiler optimizations that Java can do that C/C++ can’t do. See:

    http://www.idiom.com/~zilla/Computer/javaCbenchmark.html

    http://www.kano.net/javabench/ (see links at bottom of the page for more java vs c++ benchmarks)
    http://www.kano.net/javabench/data (good graphs)

    In my opinion, Java code is easier to write than C++ code. You might also get advantages of portability since Java code can pretty much run on any computer unmodified.

  7. Unsurprisingly, Matlab is slow. Even when Matlab code is optimized, it is at least 2 times as slow as comparable C/C++ code. Unoptimized, it can be 30 times or more slower! Of course, the good thing about Matlab is that you can easily write programs that perform math — much faster than with C/C++, Java, Python, or FORTRAN. So Matlab might be good to test an idea or implement something on a small scale.

Overall conclusion: C++ speed is about the same or only a little bit slower than FORTRAN. As C++ compilers become more optimized, the difference decreases. Java seems pretty fast, so I would probably do more research on it. If it *really* comparable to C/C++ speeds, I would probably use Java. For medium scale applications, I would use Python. For small scale applications, I would use Python or Matlab.

Star with tail

1 #

NASA/Caltech recently found a star with a tail. Well, the cool part is that the principal investigator is Chris Martin, a Caltech professor who lectured for Ph 1b and 1c: E&M and Special Relativity. He’s a nice professor although he used a lot of “um’s, and ahs’s” in his lectures. One student counted a record of over a 100 um’s and ah’s in a single hour lecture!

Tags: