comp.lang.ada
 help / color / mirror / Atom feed
From: Ted Dennison<dennison@telepath.com>
Subject: Re: short-circuit control forms
Date: Wed, 20 Jun 2001 20:15:20 GMT
Date: 2001-06-20T20:15:20+00:00	[thread overview]
Message-ID: <sv7Y6.3028$yp1.98108@www.newsranger.com> (raw)
In-Reply-To: 3B30F836.D700DAA3@raytheon.com

In article <3B30F836.D700DAA3@raytheon.com>, James A. Krzyzanowski says...
>If using "and then" and "or else" will make our software quicker, and
>then the compiler doesn't optimize any better anyway, does anybody have
>a good reason why we should NOT use short-circuit control forms?

First off, its *not* assured that it will always be quicker. Since you are
replacing one branch with one branch for each condition, it will actually result
in *more* instructions being executed if it turns out that all the conditions
need to be applied. Depending on how many conditions there are and how much work
calculating them all is, in some cases the non-short-circuit form will execute
less code even when not all of the conditions are executed.

Another thing you have to consider is that most modern processors have a branch
prediction unit (BPU) that will do speculative execution to keep the instruction
pipeline filled when branches are encountered. If the "speculation" is
incorrect, the entire pipeline has to be flushed, which slows things down
greatly. Short-circuit forms generate more branch instructions, which can amount
to more chances for the BPU to guess wrong. Again, this may be trivial compared
to the chance of not executing a lot of extra code. But if the code in
calculating the conditions is small, or its usually all executed, then it can
matter.

Another important issue is maintanence. When someone unfamiliar with that code
first tries to read it, they are probably going to expect that there's some
*reason* that those short-circuit forms had to be used, and that there's some
reason that the conditionals are in the order they are in. As an example of a
problem that could cause, suppose one conditional needs to be split out for some
reason. That may lead to unnessecary complication of the code to preserve that
(presumed-important) ordering.

My last argument against this approach is philosophical, but relates directly to
all my previous points. As a general rule, you do *not* go about randomly
optimizing code, at the expense of complexity or readability. Programs generally
do not have their workload spread out evenly over the source, so this is an
incredibly bad use of man-hours (on both ends). The proper way to optimize is to
find a part that is running too slowly, find the slowest part of that, and see
what you can do to speed it up (then repeat until satisfied). Hand-optimization
should be done with tweezers and a microsope, not with a blindfold and a
sledgehammer. 

So unless there is some *verified* speed problem with a particular conditional,
write it the way in which it makes the most sense.

---
T.E.D.    homepage   - http://www.telepath.com/dennison/Ted/TED.html
          home email - mailto:dennison@telepath.com



  reply	other threads:[~2001-06-20 20:15 UTC|newest]

Thread overview: 48+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-06-20 19:23 short-circuit control forms James A. Krzyzanowski
2001-06-20 20:15 ` Ted Dennison [this message]
2001-06-20 20:47 ` Marin David Condic
2001-06-20 22:23 ` Jeffrey Carter
2001-06-21  0:45   ` Al Christians
2001-06-21 15:06     ` Wes Groleau
2001-06-21 15:46       ` Al Christians
2001-06-21 18:28         ` Wes Groleau
2001-06-21 18:51         ` Marin David Condic
2001-06-22 12:17           ` Marc A. Criley
2001-06-22 14:55             ` Marin David Condic
2001-06-22 20:58   ` Robert Dewar
2001-06-22 21:49     ` Ted Dennison
2001-06-22 22:58     ` Jeffrey Carter
2001-06-23  0:38       ` Larry Kilgallen
2001-06-23 17:34       ` Simon Wright
2001-06-26 15:48       ` Wes Groleau
2001-06-25 17:00     ` Wes Groleau
2001-06-21  0:13 ` Mark Lundquist
2001-06-21  0:55   ` Al Christians
2001-06-21 12:39   ` Larry Kilgallen
2001-06-21 15:02   ` Wes Groleau
2001-06-21 14:24 ` short-circuit control forms (& 'long names are doom') Paul Graham
2001-06-21 17:20   ` Warren W. Gay VE3WWG
2001-06-21 18:32     ` Wes Groleau
2001-06-21 23:18   ` Charles Hixson
2001-06-22  1:01     ` Larry Kilgallen
2001-06-22  3:10     ` DuckE
2001-06-22 15:46       ` Wes Groleau
2001-06-22 19:02         ` Ted Dennison
2001-06-22 19:16         ` Ted Dennison
2001-06-22 20:53         ` Robert Dewar
2001-06-22 20:43       ` Robert Dewar
2001-06-22 22:34         ` Jerry Petrey
2001-06-25 14:30         ` Marin David Condic
  -- strict thread matches above, loose matches on Subject: below --
2001-06-20 19:50 short-circuit control forms Beard, Frank
2001-06-20 20:35 ` Ted Dennison
2001-06-20 22:32   ` Jeffrey Carter
2001-06-21  1:18     ` Mark Lundquist
2001-06-21 17:05       ` Jeffrey Carter
2001-06-21 14:31     ` Wes Groleau
2001-06-20 23:45   ` Dale Stanbrough
2001-06-20 20:57 ` Marin David Condic
2001-06-21  7:31 ` Keith Thompson
     [not found] <B6A1A9B09E52D31183ED00A0C9E0888C469BC4@nctswashxchg.nctswash.navy.mil>
2001-06-20 21:10 ` Wilhelm Spickermann
2001-06-20 22:20 Beard, Frank
2001-06-21 14:58 ` Marin David Condic
2001-06-21 17:11 ` Warren W. Gay VE3WWG
2001-06-21 17:49   ` Marin David Condic
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox