comp.lang.ada
 help / color / mirror / Atom feed
From: dewar@merv.cs.nyu.edu (Robert Dewar)
Subject: Re: Most efficient way to check for null string?
Date: 1997/06/21
Date: 1997-06-21T00:00:00+00:00	[thread overview]
Message-ID: <dewar.866897158@merv> (raw)
In-Reply-To: EC3ttx.I1o@world.std.com


Robert Duff says

<<Tucker Taft <stt@houdini.camb.inmet.com> wrote:
>...If you really want to
>shave cycles, the following will likely be the most efficient:
>
>     if Str'Last < Str'First then ...

Bletch.  That's the least readable of all, but Tucker's right that it
might be more efficient.  Tucker didn't say why.  It's because Ada
(foolishly, IMHO) allows Str's range to be something like
1_000 .. -1_000_000.  Which means that calculating the 'Length involves
a conditional jump in the general case.
>>


Of course, if a compiler wants to pay attention to the efficiency of
this construct, it can perfectly well optimize any of the three forms
into what is most efficient on the particular implementation.

Tuck's suggestion is an excellent example of what programmers should NOT do.
A programmer who uses this obscures the program, and worse, on some
implementations will slow things down! In particular there is at least
one implementation that maintains the length of strings in the descriptor,
and for such implementations, the direct test of x'Length will likely
be faster than the junk comparison above.

Trying to outguess the compiler to generate more efficient code can often
be hazardous, and indeed implementors are *most* prone to making mistakes
of this kind that are actually pessimizations.

Tuck knows too much about one particular compiler, and thus suggests 
something that would work for this one compiler. Maybe it will help on
other compilers, but maybe not :-)





  reply	other threads:[~1997-06-21  0:00 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1997-06-20  0:00 Most efficient way to check for null string? Dale Stanbrough
1997-06-20  0:00 ` Robert A Duff
1997-06-20  0:00   ` Robert Dewar
1997-06-20  0:00 ` Tucker Taft
1997-06-21  0:00   ` Robert A Duff
1997-06-21  0:00     ` Robert Dewar [this message]
1997-06-23  0:00       ` Richard Kenner
1997-06-23  0:00       ` Tucker Taft
replies disabled

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