Hold on there Buffalo Bob! There is no big difference between a strlen and
a strcmp. PL/I thoughtfully provides us with a CHAR VARYING datatype which
defines the first word as the current length. C does not do that. The
only way to see how long a string in C is, is to walk it till you find the
null. Walking the string to see if the current character is null is
equally as costly as walking the string to see if the next character is
the one you're trying to match on. The way you're describing it, you're
actually walking the strings twice: once to see how long they are and then
a second time (if they're the right length, I'll grant you) to see if
their value is the match.

The *only* way to minimize the cost of the individual character accesses 
is to write a custom finite state machine. That gets you exactly two
accesses for each byte of the string you're trying to match on.

The cost of my elseif ladder is
(cost of my string walk as many times as there are possibilities till it
matches) + (walking each string that you try to match on).

The cost of taking strlens first is
(cost of my string walk as many times as there are possibilities till it
matches) + ((walking each string that you try to match on)*2).

-- 
-Time flies like the wind. Fruit flies like a banana. [EMAIL PROTECTED]
-Stranger things have happened but none stranger than this. Steven W. Orr-
Does your driver's license say Organ Donor?Black holes are where God \
-------divided by zero. Listen to me! We are all individuals!---------

On Tue, 4 Apr 2000, Bob Bell wrote:

=>Jerry Eckert wrote:
=>
=>> Is there a more elegant way to do the equivalent of a switch statement for a
=>> character string value than a chain of if (strcmp())... else if
=>> (strcmp())... else if.... ?
=>
=>    I'll concur with many of the statements given here, and add a couple of
=>tips.
=>    One technique that I use is to first save the result of strlen.  Then you
=>only need to do a strcmp with strings of the same length.  If you normally do
=>more that one or two string comparisons on the same string, then you could get
=>a nice speed-up from this technique.
=>    If you really want to optimize things, do your own string comparision (in
=>combination with the technique above).  Look at the string one character at a
=>time and keep track of only those strings that might still possibly match.
=>Unless your strings are very close, thing would be the quickest way to do it.
=>Consider using 'register' on variables that will change often and possibly
=>inlining your comparison function.
=>
=>--
=>Bob Bell                Compaq Computer Corporation
=>Software Engineer       110 Spit Brook Rd - ZKO3-3U/14
=>TruCluster Group        Nashua, NH 03062-2698
=>[EMAIL PROTECTED]     603-884-0595


**********************************************************
To unsubscribe from this list, send mail to
[EMAIL PROTECTED] with the following text in the
*body* (*not* the subject line) of the letter:
unsubscribe gnhlug
**********************************************************

Reply via email to