Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!mailrus!uflorida!haven!mimsy!chris From: chris@mimsy.UUCP (Chris Torek) Newsgroups: comp.lang.c Subject: Re: Another silly question Message-ID: <17657@mimsy.UUCP> Date: 21 May 89 23:19:45 GMT References: <17812@cup.portal.com> <607@kl-cs.UUCP> <749@mccc.UUCP> <756@mccc.UUCP> Organization: U of Maryland, Dept. of Computer Science, Coll. Pk., MD 20742 Lines: 52 >>In article <749@mccc.UUCP> pjh@mccc.UUCP (Pete Holsberg) asked >>>why compilers produce different code for "a[i]" and "*(a+i)"? >In article <17635@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@mccc.UUCP> pjh@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@mimsy.umd.edu Path: uunet!mimsy!chris