Another silly question

Chris Torek chris at mimsy.UUCP
Mon May 22 09:19:45 AEST 1989


>>In article <749 at mccc.UUCP> pjh at mccc.UUCP (Pete Holsberg) asked
>>>why compilers produce different code for "a[i]" and "*(a+i)"?

>In article <17635 at mimsy.UUCP> I noted that
>>I have never observed one to do so.  There is no reason for a compiler
>>to generate different code, as the expressions are semantically identical.

In article <756 at mccc.UUCP> pjh at mccc.UUCP (Pete Holsberg) writes:
>Perhaps I've asked the wrong question.

Maybe not.

>I saw a couple of simple test programs that assigned 0 to each member
>of an array.  One used array subscript notation, and the other, pointer
>notation.  I compiled these >on a 7300, a 3B2/400, and a 386 running
>Microport V/386, using a variety of compilers (cc and gnu-cc on the
>7300, fpcc on the 3B2, and cc and Greenhills on the 386).

I have none of these machines, and only gcc as a compiler.  The code
produce by

	GNU C version 1.35 (vax) compiled by GNU C version 1.35.

for both loops in

	int a[20];
	main(){int i;
	for(i=0;i<20;i++)a[i]=0;
	f();
	for(i=0;i<20;i++)*(a+i)=0;
	f();
	}

was identical.  (The lack of spacing in this example is due to me
typing it in with the `cat' editor :-) )

>I ran each version and timed the execution. The subscript versions
>had different run times from the pointer versions (some slower, some
>faster!).  I assumed - perhaps naively - that the differences were
>caused by differences in code produced by the different compilers
>(and of course the hardware differences).  Was that wrong? 
>How does one account for the differences?

Differing code sequences is one of two obvious possibilities, the other
being differing multi-user loads.  The latter seems less likely, especially
if the results are repeatable.  Why not compile to assembly and compare?

If a compiler produces better code for a[i] than for *(a+i) (or vice
versa), that compiler needs work.
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris at mimsy.umd.edu	Path:	uunet!mimsy!chris



More information about the Comp.lang.c mailing list