a style question

Stan Brown, Oak Road Systems browns at iccgcc.decnet.ab.com
Fri Oct 5 00:11:52 AEST 1990


davis at pacific.mps.ohio-state.edu  (John E. Davis) writes:
> Which ['x != 100' or 'x < 100' RHS] 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.  I'd rather save the machine a few
> micro-seconds than myself since I only do the comparison once whereas the
> machine must do it many times.

I'm distressed by the emphasis on this question in quite a number of
recent postings.  The question, though answerable for each specific
machine, seems to me to be quite beside the point.  The _right_
question, IMAO, is not which takes fewer machine cycles but which is
better understood.  If program A runs a millisecond faster than program
B but does the wrong thing, A is not the better program.  If programs A
and B are both correct, A runs a millisecond faster, but A is harder to
understand, B is still the better program IMO.

When hesitating between two styles, I believe that _in general_ these
questions are the ones to ask, in descending order of priority:

    1. Which is correct?  Assuming they both are, then--
    2. Which is clearer?  (If I have to convince somebody else that this
code does what I think, which one will make the job easier?  If somebody
else has to change the code six months from now, which version will be
less likely to be changed wrongly?  If I or someone else must look at
the code a year from now and try to figure out what it does, which
version will more readily yield its secrets?   N.B. Comments can help,
but good comments do not redeem needlessly obscure code, because the
comments may not match what the code actually does!)  If neither version
is clearly clearer than the other, then--
    3. Which is faster?

Note that I said _in general_ above.   There are some qualifications.
First, in some few systems it may actually be essential to squeeze the
last microsecond out of the application.  Second, though I think 2. is
much more important than 3. in most applications, if a small increase in
complexity means a large decrease in run time I may accept the former to
get the latter.

In _The Elements of Style_, Kernighan and Plauger talked a lot about
misguided attempts by prrogrammers to write "efficient" code.  K & P's
response, shown over and over by actual benchmarks, was that clean,
readily understandable code is frequently faster than code hand 
"optimized" by the programmer.

With the example we have in hand, the question is trivial because the
chance of misunderstanding either version is pretty slim.  But in more
realistic situations, I go for clarity over "efficiency" almost every
time.  To paraphrase (or twist) Franklin, "Those who give up a little
clarity to gain a little efficiency generally lose both clarity and
efficiency."

So answering John Davis's question, which I think was not originally
his, I don't care which is faster but I'll pick x<100 rather than x!=100
because that's the way most C programmers write, therefore it's more
likely to be understood at a glance.

The above is my own opinion and not attributable to any other person or
organization.                        email: browns at iccgcc.decnet.ab.com
Stan Brown, Oak Road Systems, Cleveland, Ohio, U.S.A.    (216) 371-0043



More information about the Comp.lang.c mailing list