Fast strcmp() wanted.

Carl Edman cedman at lynx.ps.uci.edu
Sun Sep 30 02:11:18 AEST 1990


In article <1990Sep27.151543.8025 at ccs.carleton.ca> dfs at doe.carleton.ca (David F. Skoll) writes:
   In article <CEDMAN.90Sep27075013 at lynx.ps.uci.edu>
   cedman at lynx.ps.uci.edu (Carl Edman) writes:

   >      One quick dirty thing I did once was to change
   >	      if (strcmp (a,b)==0)
   >      to
   >	      if (*a==*b && (strcmp(a,b)==0))
   >
   >      I seem to remember a remarkable performance improvement, like about 5
   >      times faster.  Probably due to the fact that the program mainly did
   >      strcmp and the strcmp was pretty bad.

   Hmm... that seems strange.  If the first characters of the strings differ,
   most strcmps will not bother to test the rest.  All that the above code
   possibly saves you is a function call/return.  If this makes such a huge
   difference, then the compiler or your machine's architecture must be pretty
   bad.

I did NOT write the above original article ! Please get your attributions
right. All I wrote was one response to it, which is not quoted here.

As to the content: Yes, all that saves is the function call overhead
but that can be quite a substantial amount even on machines with good
compilers. That is why I suggested inline-ing and (under some circumstances)
a rewritten strcmp which uses longword compares. Another possibility which
comes to mind when every string is compared very often, is to create a
string structure (or better class, long live C++ ! :-) which calculates
a 32-bit CRC for each string the first time and stores it somewhere.
Then only 1 (inlined) longword-compare will do the stringcomparisons
for you.



Theorectial Physicist,N.:A physicist whose   | Send mail
existence is postulated, to make the numbers |  to
balance but who is never actually observed   | cedman at golem.ps.uci.edu
in the laboratory.                           | edmanc at uciph0.ps.uci.edu



More information about the Alt.sources mailing list