Path: utzoo!utgpu!jarvis.csri.toronto.edu!rutgers!njin!princeton!njsmu!mccc!pjh From: pjh@mccc.UUCP (Pete Holsberg) Newsgroups: comp.lang.c Subject: Re: Another silly question Message-ID: <762@mccc.UUCP> Date: 23 May 89 17:46:28 GMT References: <17812@cup.portal.com> <607@kl-cs.UUCP> <749@mccc.UUCP> <17635@mimsy.UUCP> <756@mccc.UUCP> <1677@auspex.auspex.com> Reply-To: pjh@mccc.UUCP (Pete Holsberg) Organization: The College On The Other Side of U. S. Route 1 Lines: 55 In article <1677@auspex.auspex.com> guy@auspex.auspex.com (Guy Harris) writes: =Well, if the program that used subscript notation was something like: = = for (i = 0; i < LEN; i++) = a[i] = 0; = =and the program that used pointer notation was something like: = = p = &a[0]; = while (p < &a[LEN]) = *p++ = 0; = =the answer has nothing whatsoever to do with the equivalence of "a[i]" =and "*(a + i)", since the latter program doesn't use the latter =construct, so you did ask the wrong question. So it seems! =It has, instead, to do with the fact that the equivalence of the two =constructs in question is not as trivial as the equivalence of "a[i]" =and "*(a + i)", and therefore it may be less likely that the compilers =will generate the same code for them. OK, so even though the two pieces of code are doing the same job and one uses index notation while the other uses pointer notation, the compiler is not likely to notice this. =There may well be compilers that =*do* generate the same code for them - rewrite the first loop as: = = for (i = 0; i < LEN; i++) = *(a + i) = 0; = =and then note that on most architectures, this requires that the value =in "i" be multiplied by "sizeof a[0]" before being added to the address =represented by the address of "a[0]", and do a strength reduction on ^^^^^^^^^^^^^^^^^^ could you explain this? =that multiplication; you then find the induction variable not used, and ^^^^^^^^^^^^^^^^^^ and this? =eliminate it, and by the time the smoke clears you have the loop in the =first example generating the same code as the loop in the second =example. (I don't know whether there are any compilers that do this or =not.) = =If the code generated for the two constructs is different, that could =account for performance differences. I'll try it. Thanks for the explanation. -- Pete Holsberg, Mercer County Community College, Trenton, NJ 08690 {backbone}!rutgers!njin!princeton!njsmu!mccc!pjh