2

My understanding is that Fortran (pre-90) is extremely fast in part because it does not allow pointer aliasing (and therefore allows better compiler optimization). However, I also know that pointers in C-family languages allow programmers to write extremely fast code.

I don't understand why the two languages are fast for opposite reasons. Can anyone shed some light on what's going on?

Thanks in advance.

  • 2
    Please see the very related question https://stackoverflow.com/q/146159/2737715. I'm tempted to close this one as a duplicate. – Alexander Vogt May 10 '18 at 07:27
  • The reason for both languages are fast is the low level of abstraction - meaning zero overhead for "unknown" data structures or memory management. Pointer aliasing is a small issue dealing with optimizing compiler not being able to make some decisions for caching variables. – Aki Suihkonen May 10 '18 at 07:30
  • 2
    You cannot compare speed of languages. A language does not have speed for their programs. Only particular compilers or interpreters have. I'd second the temptation for duplicate closure. The other question is all about this difference and contains many answers to choose from, often from people who don't know much about Fortran. (And I had to downvote many of them.) – Vladimir F Героям слава May 10 '18 at 08:16
  • It seems pointless to be comparing C of 20 years ago against Fortran of 30 years back. A certain proprietary dialect of C retains some old limitations but that also seems peripheral to whatever your true concern may be. – tim18 May 10 '18 at 10:43
  • I see that I'm not really asking the right question here. Thanks for the information. – Canavan Neeson May 10 '18 at 20:05
  • 1
    I think this is a reasonable question, since it leads one to the "right question," if there really is such a thing. True, there have been similar posts on SO over the last decade or so, but each adds something to the discussion. Anyway, it's interesting for those of us who don't already know "the answer", as you can see from the high view count. – Matt P May 11 '18 at 15:07

1 Answers1

5

Discussing about languages speed, and specifically optimization efficiency, is really misleading.

Let's start saying that each language has been created to simplify some specific aspects of programming scenario, and for this very reason the first goal of a programmer is the right language choice in function of the application being written.

This is the very first point, after that consider that nowadays the compilers programming use standardized tools and well defined flows, which starts from the conversion of the source to an intermediate representation on which will act the rest of the compilation chain included the optimizer. The latter happen to be the same for almost all languages, so it is realistic to expect the same result.

Practical examples can be seen taking a look to the mos diffused compilers families as GCC https://gcc.gnu.org/, LLVM https://llvm.org/, or even the .net https://en.wikipedia.org/wiki/.NET_Framework.

So the starting point, that can make a difference, is how the language is translated in the intermediary form, allowing for a better presentation to the optimization stage of the compiler chain. This depends not only from the quality of the translation phase but also from the abstraction level of the language.

We normally would think that lower the abstraction of the language, i.e. machine assembler, the better the optimization. That's absolutely wrong! Unless you are an excellent assembler programmer nothing can be done to a very bad assembler code writing.

On the contrary, precisely the high abstraction level consent to the compiler to translate the code in the more efficient way and present it to the optimizer in the most workable form.

Fortran and C are on very different levels of abstraction, the first tight enough to imply standard, and for this reason well known and pre-optimized code, the second a wide spread language, that can touch very low levels, using pointers and even inline assembler, or high level when used without abuse of any side effect.

Anyway the last C99-C11 standards have introduced many more language qualifiers that permits alignment also on some well known deficiencies like opaque pointers (https://en.wikipedia.org/wiki/Opaque_pointer) using the restrict qualifier. And in current compilers also the vectorization, usage of streaming instruction available on modern CPUs (SIMD, SSE2, etc), is vastly diffused. I.e. for the X86-64 platform the Intel C/C++ compiler is the most efficient compiler/optimizator.

Then what we have to expect for the future? With compiler's technology advancing we should expect an asymptotic nulling of any difference.

For further reading you can found also an excellent answer on Computer science stack exchange: https://scicomp.stackexchange.com/questions/203/what-makes-fortran-fast

Frankie_C
  • 4,764
  • 1
  • 13
  • 30