Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!uunet!seismo!sundc!pitstop!sun!amdcad!ames!ptsfa!ihnp4!homxb!mtuxo!mtune!codas!killer!jfh From: jfh@killer.UUCP (The Beach Bum) Newsgroups: comp.unix.questions Subject: Re: Wierd Compilers Message-ID: <1972@killer.UUCP> Date: Mon, 2-Nov-87 17:10:00 EST Article-I.D.: killer.1972 Posted: Mon Nov 2 17:10:00 1987 Date-Received: Sat, 7-Nov-87 04:21:23 EST References: <367@white.gcm> Organization: Big "D" Home for Wayward Hackers Lines: 54 Keywords: Sun Summary: Optimizer is broke? Or you didn't use it? Or it's just that way? The answer is in how the compiler is going to generate the code. Pre- and Post- decrement require different code to be generated. In article <367@white.gcm>, dc@gcm (Dave Caswell) writes: > > /* Program 1 */ > main() > { > int i = 500000; > > while(--i); > } [ code done by hand ... ] main: link #-4,a6 movl #500000,-4(a6) 1$: subl #1,-4(a6) bne 1$ | condition codes set as a side effect. rts > > /* Program 2 */ > main() > { > int i = 500000; > > while(i--); > } [ coded by hand again ... ] main: link #-4,a6 movl #50000,-4(a6) 1$: movl -4(a6),d0 subl #1,-4(a6) tstl d0 | codes got trashed after the subl. > Does anyone have a guess why program one runs in half the time of program two? > BTW this is a SUN 3. Sure, look at the assembler I am suggesting and the reason is obvious. You might want to look at the code your compiler actually generates. In general, don't post-anything when evaluating for side effects only. _Unless_ you really have to. Micro-optimizations are a loser though. - John. -- John F. Haugh II HECI Exploration Co. Inc. UUCP: ...!ihnp4!killer!jfh 11910 Greenville Ave, Suite 600 "Don't Have an Oil Well?" Dallas, TX. 75243 " ... Then Buy One!" (214) 231-0993