Path: utzoo!utgpu!water!watmath!clyde!att!osu-cis!tut.cis.ohio-state.edu!mailrus!purdue!i.cc.purdue.edu!k.cc.purdue.edu!l.cc.purdue.edu!cik From: cik@l.cc.purdue.edu (Herman Rubin) Newsgroups: comp.lang.c Subject: Re: Array indexing vs. pointers... Summary: It depends Message-ID: <934@l.cc.purdue.edu> Date: 20 Sep 88 12:38:17 GMT References: <8809191521.AA17824@ucbvax.Berkeley.EDU> <68995@sun.uucp> Organization: Purdue University Statistics Department Lines: 25 In article <68995@sun.uucp>, swilson%thetone@Sun.COM (Scott Wilson) writes: > In article <8809191521.AA17824@ucbvax.Berkeley.EDU> U23405@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@l.cc.purdue.edu (Internet, bitnet, UUCP)