To view parent comment, click here.
To read all comments associated with this story, please click here.
"Fortran beats down C (and C++, they are the same here) because it does not have pointers, or more exactly does not have the aliasing problem (different pointers pointing at the same memory address), which makes optimization by compilers so difficult."
This is not really true. Since Fortran 90, the language has added support for pointers which basically work as in C but are less extensively used compared to the C and less powerful, for example pointer arithmetic is not supported, no pointer to a pointer to a variable declaration, no function pointers.
So declaring a pointer to a real number would be written in Fortran 90 as:
REAL, POINTER :: p1
Making p1 to point to a variable var would give
p1 => var.
So pointers exist in Fortran, they are less powerful than in C but the fortran syntax is less complex and also the language adds several nice features as, the possibility of testing the association status of a pointer to a variable, the possibility to disassociated a pointer to a target and a TARGET attribute that allows the compiler to know that a particular variable could be pointed by a pointer and therefore it must not be optimized out of existence.
Pointers in Fortran can poin to any variable type, to derived data types and to arrays. They can be used to implement dynamic memory allocation and to construct various types of dynamic data structures.
So now why Fortran is faster than C in numerical code? Well i don't think it is a language related question (C is quite a fast and low level language), i rather think that compilers make all the difference here. I mean, Fortran compilers are built and optimized to create the fastest numerical code possible. They are designed to produce only fast numerical code. C or C++ compilers have a larger field of application, they compile very different king of applications where optimizations strategies can be very different.
Fortran compilers are number crunching, number crunching, number crunching, they have been built for that for several decades... they know only that but they do it very well.
Also besides the performance question, Fortran is by far the most used language for coding numerical applications among scientists because it offers a lot of features that are important for this audience. Matrix representation, direct matrix multiplication, better implementation for complex numbers, more flexible manipulation of arrays, easier to read, easier to port, easier to maintain. All of those make this language a very attractive solution for scientists and engineers who code numerical applications.
Edited 2008-08-21 10:26 UTC






Member since:
2005-11-11
Nothing to do with precision. Fortran use the same format than C those days on common machines (IEEE floating point); if you implement the same algorithm, you can in theory have exactly the same results (although differences with compilers mean this will never happen in practice). There is a lot of expertise in fortran numerical code, though, which is why you still have so much code implemented in fortran and used today (blas, lapack, optimization, etc...).
Fortran beats down C (and C++, they are the same here) because it does not have pointers, or more exactly does not have the aliasing problem (different pointers pointing at the same memory address), which makes optimization by compilers so difficult. That's why Fortran is still much faster than C or C++ on most benchmarks.
It has also an array concept: that's why most C++ is a really poor language IMO for numerical code, because everybody has a different, incompatible class for vectors, so the speed you gain from being in C++ is lost converting formats between libraries. This and the fact that the language is way too complicated IMO. I personally believe that C++ is a niche language: it is still the only "industry" language you can use when you need ten of thousand objects touching each other (heavy GUI, games), but I would never use C++ if I had a valid other choice.