Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!uunet!husc6!necntc!ima!haddock!karl From: karl@haddock.ISC.COM (Karl Heuer) Newsgroups: comp.unix.questions,comp.lang.c Subject: Re: Wierd Compilers Message-ID: <1635@haddock.ISC.COM> Date: Fri, 6-Nov-87 14:28:01 EST Article-I.D.: haddock.1635 Posted: Fri Nov 6 14:28:01 1987 Date-Received: Sun, 8-Nov-87 16:32:52 EST References: <367@white.gcm> <18964@amdcad.AMD.COM> <44177@beno.seismo.CSS.GOV> <1988@killer.UUCP> Reply-To: karl@haddock.ima.isc.com (Karl Heuer) Followup-To: comp.lang.c Organization: Interactive Systems, Boston Lines: 25 Xref: mnetor comp.unix.questions:4826 comp.lang.c:5314 [Someone noted that "while (i--);" is slower than "while (--i);". It was pointed out that the former generates more instructions because of the extra work to save the old value of the counter. At least one person claimed that the postdecrement operator should be avoided for this reason; apparently he didn't notice that the value was being used, and that the two statements are not semantically identical. Rick Adams observed that, even if the value is not being used, the user need not be concerned with this optimization.] More than one person writes that an n-trip loop should be written ++n; while (--n) ...; I'm surprised that nobody has yet mentioned the idiom that I use: while (--n >= 0) ...; This has the speed of predecrement, and avoids the kludge of having an extra increment. It requires n to be a signed type, but that's not usually a problem. (Also, it doesn't have identical semantics if n is negative, but from the context I presume that this is not a concern.) Of course, the other way is to write do ...; while (--n != 0); [or] do ...; while (--n > 0); assuming n is known to be initially nonzero. Karl W. Z. Heuer (ima!haddock!karl or karl@haddock.isc.com), The Walking Lint Followups to comp.lang.c only.