Path: utzoo!news-server.csri.toronto.edu!cs.utexas.edu!uunet!brunix!gvr From: gvr@cs.brown.edu (George V. Reilly) Newsgroups: comp.lang.c Subject: Re: Efficient STRing CoMPares? Message-ID: <68822@brunix.UUCP> Date: 17 Mar 91 01:38:02 GMT References: <1991Mar15.142821@mars.mpr.ca> Sender: news@brunix.UUCP Reply-To: gvr@cs.brown.edu (George V. Reilly) Organization: Brown University, Dept. of Computer Science Lines: 23 In article <1991Mar15.142821@mars.mpr.ca> stone@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@cs.brown.edu +1 (401) 863-7684 uunet!brunix!gvr gvr@browncs.bitnet Box 1910, Brown U, Prov, RI 02912