Efficient STRing CoMPares?

George V. Reilly gvr at cs.brown.edu
Sun Mar 17 11:38:02 AEST 1991


In article <1991Mar15.142821 at mars.mpr.ca> stone at mars.mpr.ca (Darren Stone) writes:
% 
% Does anyone know if there's a better (!?) way to compare
% strings than with strcmp()?
% 
% I'm looking for any speed advantage I can get!

If you're comparing strings for equality, you could use the
following macro:

#define STREQ(a,b) (*(a) == *(b) && (*(a) == '\0' || strcmp((a)+1,(b)+1) == 0))

which only calls strcmp() if the two strings agree in the first
character and if they're not zero-length strings.  Thus, for most
string comparisons, strcmp will not be called.  Unless the
implementors of your libraries really botched it, strcmp()
should already be extremely efficient.

You should be able to work out STRLT, STRLE, STRNE, STRGE, and STRGT
(if you need them) based on the above example.
________________
George V. Reilly   `Toad of Toad Hall'	gvr at cs.brown.edu   +1 (401) 863-7684
uunet!brunix!gvr   gvr at browncs.bitnet	Box 1910, Brown U, Prov, RI 02912



More information about the Comp.lang.c mailing list