Fast strcmp() wanted.

Carl Edman cedman at lynx.ps.uci.edu
Fri Sep 28 00:50:17 AEST 1990


In article <OTTO.90Sep27145643 at tukki.jyu.fi> otto at tukki.jyu.fi (Otto J. Makela) writes:
   In article <1646 at cherry.edc.UUCP> fraser at edc.UUCP (Fraser Orr) writes:
      In article <12145 at crdgw1.crd.ge.com> larocque at jupiter.crd.ge.com (David M. LaRocque) writes:
      >After I profiled my C program I discovered that the function
      >strcmp() takes one third of my program's CPU time.  I was hoping
      >someone may have written their own version of strcmp() that
      >outperforms the library's function.

      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.

   This should obviously be:
	   if(*a==*b && (strcmp(a,b)==0))
   (just in case...)

   I'd believe if your compiler is really brain-damaged, this could also help
   a teeny weeny bit:
	   if(*a==*b && (!strcmp(a,b)))

If you use a good compiler (like f.e. gcc) you should get an improvement
better than the above by simply inline-ing a self-written strcmp function.
Another thing oyu might consider and which would be useful for some
types of machines like f.e. 680x0 , would be to do the first few cmp-s
as byte-cmps until you get to a longword boundary, after which you do
longword compares. When you have got long strings which often match in
the first part (or you often compare matching strings) then this could
give you a really remarkable preformance improvement.
Maybe the best way to fix your problem would be to find our if you REALLY
need that many strcmp-s. If you f.e. do some kind of parseing and simply
check the text against a list of keywords there are MANY better solutions,
from sort-ing the keyword list and using a binary search, to using a
hash-function (there are programms which can help you create them), to
using lex.


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