[comp.lang.fortran] pointers vs arrays

3003jalp@ucsbuxa.ucsb.edu (Applied Magnetics) (11/30/90)

(C vs Fortran:  the wars are-a-ragin'...)

It is not obvious that indexing in an array is slower than
dereferencing a pointer.  Of these three examples,

               INDEXING                             DEREFERENCING
 1: FORTRAN              2: C                         3: C
    do 10 i= 1, n     for(i=0; i<n; i++)         for(ptr=x+n; (ptr--)>x;)
10  x(i)= 0           x[i]= 0;                   *ptr= 0;

the first two may run as fast as the third.   I know for a fact that
the MC8100, a RISC cpu, has an addressing mode for <base register>
+<offset register>*<1,2,4 or 8> with no time penalty.  The gnu
C compiler would use that addressing mode with example 2.  In
multi-array operations (e.g. ArrX= ArrY+ArrZ) an indexing loop would be
_faster_ because it would increment one index instead of several
pointers.

I gave up using common sense to predict program efficiency long ago.
If you can look at the generated assembler, you may be surprised.
With pipelines stalls, cache misses, vector slots misses and the like,
who knows what will happen anyway.

I like C myself.  Give me a good optimizing C compiler and I'll use
it.  Give me a better optimizing Fortran and, shucks, I'll use it
instead.
  --Pierre Asselin, R&D, Applied Magnetics Corp.  I speak for me.