Array indexing vs. pointers...

Herman Rubin cik at l.cc.purdue.edu
Tue Sep 20 22:38:17 AEST 1988


In article <68995 at sun.uucp>, swilson%thetone at Sun.COM (Scott Wilson) writes:
> In article <8809191521.AA17824 at ucbvax.Berkeley.EDU> U23405 at UICVM (Michael J. Steiner) writes:
> >First, I have a question. How and why is array indexing slower than
> >pointer arithmetic? They are very similar. Also, I think that compilers
> >should automatically translate array indexing into pointer arithmetic when
> >dealing with arrays in C. Any comments, opinions?

This has appeared before, but maybe it bears repeating.  

It depends on the architecture and on where the pointers are located
(registers vs. memory).  Consider copying an array.  If the pointers are
in registers and reading and writing *(j++) is hardware, pointers will beat
indexing.  This is the case on the VAX, but not on the PYRAMID.

If, on the other hand, the array bases and the index are in registers, and
register indexing is _hardware_, array indexing will beat pointers if the 
previous situation does not hold.  Reverse the machines above.

The answer depends on the hardware, the storage classes, the timing, and
for machines which have instruction overlap, on the overlaps available.
An attempt to force the issue is WRONG.
-- 
Herman Rubin, Dept. of Statistics, Purdue Univ., West Lafayette IN47907
Phone: (317)494-6054
hrubin at l.cc.purdue.edu (Internet, bitnet, UUCP)



More information about the Comp.lang.c mailing list