From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,874f90f0816ffe3b X-Google-Attributes: gid103376,public From: dewar@merv.cs.nyu.edu (Robert Dewar) Subject: Re: Most efficient way to check for null string? Date: 1997/06/21 Message-ID: #1/1 X-Deja-AN: 251571680 References: <5oe038$2d0$1@goanna.cs.rmit.edu.au> Organization: New York University Newsgroups: comp.lang.ada Date: 1997-06-21T00:00:00+00:00 List-Id: Robert Duff says < 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 :-)