Pointers vs. Arrays: an aside about - (nf)

bobo at inmet.UUCP bobo at inmet.UUCP
Fri Oct 5 15:07:24 AEST 1984


#R:watmath:-922400:inmet:5000024:000:1238
inmet!bobo    Oct  3 13:23:00 1984

>>for maximum efficiency, I have to write:
>>	{ struct foo *foop = fooarr;
>>	  for (i=0, i < FOOMAX; i++)
>>		dostuff(*(foop++));
>>	}
>>instead of what I mean:
>>	for (i=0; i < FOOMAX; i++)
>>		dostuff(fooarr[i]);
>>
>>	Roger Hayes
>Only on some machines does the former generate better code. Even on a Vax,
>for some storage class and type combinations, the amount of code would
>be the same (but the former might run *slower* cuz it is incrementing
>two variables each time around the loop). On some machines, pointer handling
>(other than just plain indirection) is so cumbersome that the second piece
>of code is always better.
>                 Kevin Martin, UofW Software Development Group
>----------

I think the point of the strength reduction optimization here is the
removal of the implicit multiplication in the array reference. Now
if the length of "foo" happened to be a power of two and the compiler
was moderately smart you might get a shift. In the general case, however,
replacing the multiplication within the loop with the addition should 
outweigh any extra pointer handling stuff.
				    Mark Friedman
				    Intermetrics, Inc.
				    ...harpo!inmet!bobo
				    ...esquire!inmet!bobo
				    ...ima!inmet!bobo



More information about the Comp.lang.c mailing list