comp.lang.ada
 help / color / mirror / Atom feed
* Re: Most efficient way to check for null string?
  1997-06-20  0:00 Most efficient way to check for null string? Dale Stanbrough
  1997-06-20  0:00 ` Tucker Taft
@ 1997-06-20  0:00 ` Robert A Duff
  1997-06-20  0:00   ` Robert Dewar
  1 sibling, 1 reply; 8+ messages in thread
From: Robert A Duff @ 1997-06-20  0:00 UTC (permalink / raw)



In article <5oe038$2d0$1@goanna.cs.rmit.edu.au>,
Dale Stanbrough  <dale@goanna.cs.rmit.EDU.AU> wrote:
>I think the following are both equally readable, so which is likely 
>to be more efficient?
>
>	if Str = "" then
>	
>	if Str'Length = 0 then

I find the first slightly more readable.  I suspect they're very close
to the same efficiency.  I suspect that the second is at least as
efficient as the first (i.e. I would be surprised if a compiler did
worse on #2, but I would not be surprised if they were equal, or maybe
#2 slightly better).

The only sensible way to answer this question is to measure it.

It's quite easy to write a compiler where #1 is as efficient as #2.  If
a given compiler doesn't do that, I take it as an indication that its
customers don't care about this sort of thing.

- Bob




^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Most efficient way to check for null string?
  1997-06-20  0:00 Most efficient way to check for null string? Dale Stanbrough
@ 1997-06-20  0:00 ` Tucker Taft
  1997-06-21  0:00   ` Robert A Duff
  1997-06-20  0:00 ` Robert A Duff
  1 sibling, 1 reply; 8+ messages in thread
From: Tucker Taft @ 1997-06-20  0:00 UTC (permalink / raw)



Dale Stanbrough (dale@goanna.cs.rmit.EDU.AU) wrote:

: I think the following are both equally readable, so which is likely 
: to be more efficient?

: 	if Str = "" then
: 	
: 	if Str'Length = 0 then

As usual, it depends on the implemenation.  I suspect the second will
be the more efficient on most compilers.  If you really want to
shave cycles, the following will likely be the most efficient:

     if Str'Last < Str'First then ...


: Dale

-Tucker Taft   stt@inmet.com   http://www.inmet.com/~stt/
Intermetrics, Inc.  Burlington, MA  USA




^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Most efficient way to check for null string?
  1997-06-20  0:00 ` Robert A Duff
@ 1997-06-20  0:00   ` Robert Dewar
  0 siblings, 0 replies; 8+ messages in thread
From: Robert Dewar @ 1997-06-20  0:00 UTC (permalink / raw)



Robert Duff said

<<It's quite easy to write a compiler where #1 is as efficient as #2.  If
a given compiler doesn't do that, I take it as an indication that its
customers don't care about this sort of thing.>>


It's not quite so simple. The business of optimizing special cases is a never
ending task. Obviously all customers will say they want the code as fast
as possible, but they have two other requirements:

1) they do not want to pay infinite money
2) they do not want to wait infinite time

therefore the desire to optimize as much as possible has to be balanced
against other requirements.

What you can deduce if a compiler does not optimize the comparison with
the null string is that it has not yet been decided that this is a significant
enough optimization to be worth while, or more likely that it has never come
up, which is a bit different from your conclusion, generalized apparently
to all possible similar optimizations ("this sort of thing")





^ permalink raw reply	[flat|nested] 8+ messages in thread

* Most efficient way to check for null string?
@ 1997-06-20  0:00 Dale Stanbrough
  1997-06-20  0:00 ` Tucker Taft
  1997-06-20  0:00 ` Robert A Duff
  0 siblings, 2 replies; 8+ messages in thread
From: Dale Stanbrough @ 1997-06-20  0:00 UTC (permalink / raw)



I think the following are both equally readable, so which is likely 
to be more efficient?

	if Str = "" then
	
	if Str'Length = 0 then


Dale




^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Most efficient way to check for null string?
  1997-06-20  0:00 ` Tucker Taft
@ 1997-06-21  0:00   ` Robert A Duff
  1997-06-21  0:00     ` Robert Dewar
  0 siblings, 1 reply; 8+ messages in thread
From: Robert A Duff @ 1997-06-21  0:00 UTC (permalink / raw)



In article <EC3C0x.E68.0.-s@inmet.camb.inmet.com>,
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.

- Bob




^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Most efficient way to check for null string?
  1997-06-21  0:00   ` Robert A Duff
@ 1997-06-21  0:00     ` Robert Dewar
  1997-06-23  0:00       ` Tucker Taft
  1997-06-23  0:00       ` Richard Kenner
  0 siblings, 2 replies; 8+ messages in thread
From: Robert Dewar @ 1997-06-21  0:00 UTC (permalink / raw)



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 :-)





^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Most efficient way to check for null string?
  1997-06-21  0:00     ` Robert Dewar
@ 1997-06-23  0:00       ` Tucker Taft
  1997-06-23  0:00       ` Richard Kenner
  1 sibling, 0 replies; 8+ messages in thread
From: Tucker Taft @ 1997-06-23  0:00 UTC (permalink / raw)



Robert Dewar (dewar@merv.cs.nyu.edu) wrote:

: 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.  ...

: Tuck's suggestion is an excellent example of what programmers should NOT do.

I guess I forgot the smiley ;-).

Please don't write "if Str'Last < Str'First then ..." in your code,
but of course if you must, name it after me... (insert smiley here).

-Tucker Taft   stt@inmet.com   http://www.inmet.com/~stt/
Intermetrics, Inc.  Burlington, MA  USA




^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Most efficient way to check for null string?
  1997-06-21  0:00     ` Robert Dewar
  1997-06-23  0:00       ` Tucker Taft
@ 1997-06-23  0:00       ` Richard Kenner
  1 sibling, 0 replies; 8+ messages in thread
From: Richard Kenner @ 1997-06-23  0:00 UTC (permalink / raw)



In article <dewar.866897158@merv> dewar@merv.cs.nyu.edu (Robert Dewar) writes:
>>...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.

That's true, but the condition on the conditional jump is precisely the
same as the condition of the string being null, so the tests should be
equivalent.  Out of curiousity, I checked to see what GNAT will do.  It
neither optimized it to the above test, nor did the general computation of
'Length (involving conditional jumps) and tested its resul, but instead
simply did the above test twice in a row!

That redundant test should have been eliminated by the GCC optimizer
and I just put on my list to find out one of these days why it wasn't.




^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~1997-06-23  0:00 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1997-06-20  0:00 Most efficient way to check for null string? Dale Stanbrough
1997-06-20  0:00 ` Tucker Taft
1997-06-21  0:00   ` Robert A Duff
1997-06-21  0:00     ` Robert Dewar
1997-06-23  0:00       ` Tucker Taft
1997-06-23  0:00       ` Richard Kenner
1997-06-20  0:00 ` Robert A Duff
1997-06-20  0:00   ` Robert Dewar

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