Search code examples
cperformancepointersfortranfortran77

Role of pointers in C and Fortran in determining program speed


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.


Solution

  • 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