comp.lang.ada
 help / color / mirror / Atom feed
From: Gautier Write-only <gautier@fakeaddress.nil>
Subject: Re: Short circuit boolean evaluation
Date: Fri, 07 Nov 2003 01:49:56 +0100
Date: 2003-11-07T01:49:56+01:00	[thread overview]
Message-ID: <3FAAEC34.6E67380D@fakeaddress.nil> (raw)
In-Reply-To: 87f5a614.0311051528.30450c7a@posting.google.com

svaa:

> I'm a newbie in Ada, I've worked in several languages, last three
> years I've worked in Delphi (Pascal).

Welcome to professional programming...

Well. The efficiency of the "short circuit" is to be relativized,
especially with the processors of the last 10 years where each
condition (that automatically come with the short-circuit) means
a serious slowdown. This "short circuit is faster"-mania is completely
outdated. You find it in Delphi simply because it was already brought
into Turbo Pascal 20 years ago as a marketing argument. It worked
well on the PCs of these days and with a non-optimizing compiler
like Borland's. You'll find rapidly that
most of the time "and" and "or" as complete evaluation is better
and some cases like "a_boolean_variable and a_long_function"
naturally deserve the "and then", and of course "and then" means
clearly a short circuit. No problem there. Think also that a compiler
can also optimize much better "and" than "and then". It's also
good to see the assembler output and of course perform some comparative
timings. You'll be surprised...
Generally, what makes more sense "at reading" is also the most efficient.
If you want to save microseconds, tune the code...

[...]
> Standard pascal has full evaluation, but most compilers have an option
> for short circuit evaluation, and it's the default one, that should
> give a clue.

If you think to Delphi, it's wrong: the behaviour is set by an option
that you can default to what you want. In fact your program may have
a completely different behaviour if the compiler with which you compile
it has the checkbox "Complete Boolean Evaluation" set or not (see your
own words about side effects...). The {$B+} or {$B-} you can put
in the source is not a lot better (especially in include file).
This point is surely one of the worse mistakes (out of many) Borland
did in its language design. What was a nice gadget at its time has
become a burden because you have to put everywhere {$B+}'s or {$B-}'s
if you want to obtain a real efficiency. Frankly, "and then" and "or else"
are a bit more convenient, aren't they ?...
Not that standard Pascal has "and_then" and "or_else" for short circuits.

> Programs will have the same result if you change the behavior to short
> circuit, but you will save a lot of "then" and "else".
> 
> Well, I'm missing the case of expresions that call functions. If that
> function have side efects, you have to evaluate it. I think this is a
> bad coding style, but perhaps it's because I usually work with
> short-circuit. In this cases you would lose backward compatibility.
> Although it could be solved with a pragma.

You have little chance to bring Borland's blunders into Ada.
The best solution is to wait a bit of time. Borland makes you very
nostalgic (I know this feeling), but you will see, it will fade...
________________________________________________________
Gautier  --  http://www.mysunrise.ch/users/gdm/gsoft.htm

NB: For a direct answer, e-mail address on the Web site!



  parent reply	other threads:[~2003-11-07  0:49 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-11-05 23:28 Short circuit boolean evaluation svaa
2003-11-06  3:49 ` Steve
2003-11-06 19:44   ` svaa
2003-11-06 23:03     ` tmoran
2003-11-07  0:26     ` Mark Lorenzen
2003-11-07 21:27       ` Simon Wright
2003-11-07 22:59         ` Mark Lorenzen
2003-11-07  2:29     ` Wes Groleau
2003-11-07  3:48     ` Steve
2003-11-10 11:18     ` Georg Bauhaus
2003-11-07  0:49 ` Gautier Write-only [this message]
2003-11-07  3:03   ` Alexandre E. Kopilovitch
2003-11-10 17:36   ` Warren W. Gay VE3WWG
replies disabled

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