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

kpmartin@watmath.UUCP (Kevin Martin) (09/30/84)

>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

geoff@desint.UUCP (Geoff Kuenning) (10/09/84)

>>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....On some
>machines, pointer handling (other than just plain indirection) is so
>cumbersome that the second piece of code is always better.
>                 Kevin Martin

Which is precisely the point.  The programmer writing the code can't know
what the most efficient method is;  the compiler can, and should take
advantage of this knowledge to produce better code.

Of course, we could always put in #if vax || prime || ... :-)
-- 
	Geoff Kuenning
	First Systems Corporation
	...!ihnp4!trwrb!desint!geoff