Newsgroups: comp.lang.c Path: utzoo!henry From: henry@zoo.toronto.edu (Henry Spencer) Subject: Re: a style question Message-ID: <1990Oct1.174941.22195@zoo.toronto.edu> Organization: U of Toronto Zoology References: <7341@darkstar.ucsc.edu> <1990Sep30.050655.13212@zoo.toronto.edu> <1990Sep30.172917.2951@Neon.Stanford.EDU> Date: Mon, 1 Oct 90 17:49:41 GMT In article davis@pacific.mps.ohio-state.edu (John E. Davis) writes: > [ `<' vs `!=' in comparisons ] >Which generates faster code? It seems to me that it is easier to tell if two >values are unequal than to tell if one is greater than the other... Most machines do all comparisons in the same length of time. When it does make a difference, the nature of the difference is highly machine- specific. What *can* make a difference is to run the loop backwards, algorithm permitting: for (x = 99; x >= 0; x--) ... so that the termination test is a comparison against zero. That very often *is* faster than comparison to an arbitrary constant. When the algorithm requires an up-counter, it can even be faster to run a down- counter in parallel and use it for the termination test: for (i = 99, x = 0; i >= 0; i--, x++) /* code using x */ Obviously this depends somewhat on the availability of registers and other machine-specific details. -- Imagine life with OS/360 the standard | Henry Spencer at U of Toronto Zoology operating system. Now think about X. | henry@zoo.toronto.edu utzoo!henry