Fast strcmp() wanted.

Russell Poffenberger poffen at sj.ate.slb.com
Fri Sep 28 07:32:53 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)))

The reason for a performance increase is that statistically, for random
strings, more often than not, the first character will not match, so that
further checking is not needed. In the example given, this avoids making a
function call and the overhead associated with it.

Creating inline functions for strcmp type operations (if your compiler supports
them) will help a lot by avoiding a function call entirely.

Russ Poffenberger               DOMAIN: poffen at sj.ate.slb.com
Schlumberger Technologies       UUCP:   {uunet,decwrl,amdahl}!sjsca4!poffen
1601 Technology Drive		CIS:	72401,276
San Jose, Ca. 95110             (408)437-5254



More information about the Alt.sources.d mailing list