[net.lang.c] Pointers vs. Arrays: an aside about

bobo@inmet.UUCP (10/05/84)

#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