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:
- 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++.
- 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 - 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.
- 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.
- 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. - 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.
- 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.
7 Comments
Good thing my scientific programming class this term teaches all of those - FORTRAN, C, C++, Matlab, and Mathematica. Woo! With Java already down, I’ll be able to handle all those simulations no problem! (I just wish I could take a class in IDL and TCL, those are the two that every astronomer seems to use for 95% of everything…)
Interesting analysis, though.
Wow, I’m impressed! Soon, you’ll know more about programming than I do! Also, that’s a cool class :).
A couple of potentially obviously comments, not really disagreeing with what you wrote:
In general, the closer you get to writing machine code, the more efficiently your
program is going to run. The benefits of using higher level languages are the ability to
program more rapidly, apply various levels of formal methods from type checking to
correctness proofs, and add convenient features like automatic garbage collection.
The theme is trading precise control over what the machine does for code that is robust
and easy to write and understand.
Of course there are optimizing compilers for high level languages like Python and Java
(even Javascript), but in principle you can’t do much better than writing C, FORTRAN, or
assembly.
As for portability, I don’t think you can necessarily say that Java is all
that much more portable than well-written C (code that doesn’t make assumptions
about the architecture, if necessary uses the architecture type as a parameter, follows
standards, etc.). Java can be just as hard to port between different systems (consider
porting azureus to an embedded device with a Java VM). I definitely wouldn’t say that
Java is more portable than say Python.
I’m taking compilers this semester, so I’ll probably know more about this stuff in
December :).
Just thought I’d add: I’ve just been putting together a Python hackjob for a website (http://web.mit.edu/c-entry/) inherited from a mad 1337 h4xx0r, and it runs really slowly. This little program that assembles the webpages with the header is like 16 KB long and it takes upwards of two minutes to run. Yikes, Java never took that long unless I was going full hardcore GUI on its ass.
From The Benchmarks Game homepage http://shootout.alioth.debian.org/
“How can we benchmark a programming language?
We can’t - we benchmark programming language implementations.”
Because of license conditions we don’t feel able to benchmark Intel Fortran or Sun Studio 12 Fortran or … The language implementation matters!
“How can we benchmark language implementations?
We can’t - we measure particular programs.”
Do they look much like typical scientific programs?
Fortran IV is great. Give me a grant to continue using it. I seriously thought of trying to get my work approved, at least for a minor thesis: reviewing code I wrote in Fortran IV in 1976, and explaining the design methods I used. I recently saw some literature from the late 1970’s which explained that labelled common can be used for “information hiding”.
Is there anyone out there who knows Fortran IV, and knows a course where I could get my work examined?
I have spent a lot of time learning C and C++ and mnay other languages. I still think Fortran IV is a wonderful language and am amazed to read recently that it is faster than C++ and can even be faster than C.
To its credit, C has been used to implement Unix which was a wonderful invention. But Unix could be written in Fortran.