On Tue, 4 Apr 2000, Steven W. Orr wrote:
> There is no big difference between a strlen and a strcmp.
Well, strlen() will likely push something onto the stack, while strcmp() has
to do an additional register load per character, and strcmp() can return
significantly earlier if you've got a significant difference in string
lengths, but other then that, they're largely the same, assuming register
loads and register stores have equal cost on your machine.
Yes, I'm being pedantic. So are you. ;-)
> 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.
I think the point was that, if you have a non-trivial number of possible
matches, and the match lengths vary, you can call strlen() on your candidate
once and quickly cut out a large number of comparisons. You'll have to
calculate the match lengths "by hand", of course, but if you're optimizing for
speed...
> The *only* way to minimize the cost of the individual character accesses
> is to write a custom finite state machine.
I know a balanced binary tree would be significantly faster then a
brute-force repeated comparison, assuming the number of possible matches is
non-trivial. While I don't really know much about state machines, I don't
think a binary tree (which is a data structure) qualifies as one...?
On Tue, 4 Apr 2000, Steven W. Orr wrote:
> I beg to heartily differ. *No* C compiler will evaluate a call to strlen
> at compile time, even if the string is compiletime evaluatable.
I think Borland's current C/C++ compiler will, if you have all the
optimizations turned on. (It has features which effectively turn the C string
functions into language primitives for optimization purposes.)
Optimizing compilers can do some (neat|helpful|annoying|mind-bending)
things. :-)
--
Ben Scott <[EMAIL PROTECTED]>
| Why are things carried by car called shipments? |
| Why are things carried by ships called cargo? |
**********************************************************
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
**********************************************************