a style question
Henry Spencer
henry at zoo.toronto.edu
Tue Oct 2 03:49:41 AEST 1990
In article <DAVIS.90Oct1025438 at pacific.mps.ohio-state.edu> davis at 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 at zoo.toronto.edu utzoo!henry
More information about the Comp.lang.c
mailing list