[comp.lang.c] string equality/comparison optimization

kyle@xanth.UUCP (07/07/87)

In article <1711@umn-cs.UUCP>, herndon@umn-cs.UUCP (Robert Herndon) writes:
> In article <8011@brl-adm.ARPA>, Alan_Cote.DlosLV-Comm@Xerox.COM writes:
> > >Henry Spencer <henry@utzoo.uucp> suggests (assuming a and b have
> > >no side effects)
> > >>#define    STREQ(a, b)    (*(a) == *(b) && strcmp((a), (b)) == 0)
> > >>This makes a considerable speed difference in programs that use strings
> > >>a lot, since string comparisons usually fail on the very first character.
>   This will also greatly slow a good many programs down on machines
> that do not support byte addressing.

But then wouldn't the comparison be just as slow using strcmp(), since it too
must compare bytes?

kyle jones   <kyle@xanth.cs.odu.edu>   old dominion university, norfolk, va

tim@amdcad.AMD.COM (Tim Olson) (07/08/87)

In article <1500@xanth.UUCP> kyle@xanth.UUCP writes:
+-----
| In article <1711@umn-cs.UUCP>, herndon@umn-cs.UUCP (Robert Herndon) writes:
| >   This will also greatly slow a good many programs down on machines
| > that do not support byte addressing.
| 
| But then wouldn't the comparison be just as slow using strcmp(), since it too
| must compare bytes?
+-----

It doesn't necessarily have to compare the strings byte-by-byte.  If the
processor is "big-endian," the comparison can occur a word at a time
until the terminating zero-byte is reached (this is how the Am29000 does
it -- see lengthy discussion in comp.arch & this news group about 3
months ago). However, even with a very slight overhead in procedure
call/return, it probably is still a benificial optimization to
explicitly compare the first characters before calling strcmp(). 


	-- Tim Olson
	Advanced Micro Devices
	(tim@amdcad.amd.com)

gwyn@brl-smoke.UUCP (07/09/87)

In article <1500@xanth.UUCP> kyle@xanth.UUCP (Kyle Jones) writes:
>But then wouldn't the comparison be just as slow using strcmp(), since it too
>must compare bytes?

Not necessarily.  An old trick used in some implementations of memcpy()
on systems that don't have special block-move instructions can also be
used for strcmp():  Take care of the individual chars until one pointer
has been incremented so that it is word-aligned; then, if the second
pointer is also word-aligned, enter a special loop that does full-word
comparisons, with a bit of extra code to handle the loop termination
correctly.

I don't recommend this, however, unless you're expecting to handle
mostly long strings.  Since that's unlikely, the odds are that the
extra bookkeeping overhead won't be justified for general adoption.