Path: utzoo!censor!geac!torsqnt!news-server.csri.toronto.edu!cs.utexas.edu!sun-barr!apple!julius.cs.uiuc.edu!zaphod.mps.ohio-state.edu!swrinde!ucsd!hub.ucsb.edu!ucsbuxa!3003jalp From: 3003jalp@ucsbuxa.ucsb.edu (Applied Magnetics) Newsgroups: comp.lang.fortran Subject: pointers vs arrays Message-ID: <7495@hub.ucsb.edu> Date: 29 Nov 90 18:16:37 GMT Sender: news@hub.ucsb.edu Distribution: comp Lines: 27 (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; ix;) 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 +*<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. Brought to you by Super Global Mega Corp .com