home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 1 / 1873 < prev    next >
Internet Message Format  |  1990-12-28  |  3KB

  1. From: cedman@lynx.ps.uci.edu (Carl Edman)
  2. Newsgroups: alt.sources
  3. Subject: Re: Fast strcmp() wanted.
  4. Message-ID: <CEDMAN.90Sep27075013@lynx.ps.uci.edu>
  5. Date: 27 Sep 90 14:50:17 GMT
  6.     <OTTO.90Sep27145643@tukki.jyu.fi>
  7. Organization: non serviam
  8. Lines: 45
  9. Nntp-Posting-Host: lynx.ps.uci.edu
  10. In-reply-to: otto@tukki.jyu.fi's message of 27 Sep 90 12:56:43 GMT
  11.  
  12. In article <OTTO.90Sep27145643@tukki.jyu.fi> otto@tukki.jyu.fi (Otto J. Makela) writes:
  13.    In article <1646@cherry.edc.UUCP> fraser@edc.UUCP (Fraser Orr) writes:
  14.       In article <12145@crdgw1.crd.ge.com> larocque@jupiter.crd.ge.com (David M. LaRocque) writes:
  15.       >After I profiled my C program I discovered that the function
  16.       >strcmp() takes one third of my program's CPU time.  I was hoping
  17.       >someone may have written their own version of strcmp() that
  18.       >outperforms the library's function.
  19.  
  20.       One quick dirty thing I did once was to change
  21.           if (strcmp (a,b)==0)
  22.       to
  23.           if (*a==*b && (strcmp(a.b)==0))
  24.  
  25.       I seem to remember a remarkable performance improvement, like about 5
  26.       times faster.  Probably due to the fact that the program mainly did
  27.       strcmp and the strcmp was pretty bad.
  28.  
  29.    This should obviously be:
  30.        if(*a==*b && (strcmp(a,b)==0))
  31.    (just in case...)
  32.  
  33.    I'd believe if your compiler is really brain-damaged, this could also help
  34.    a teeny weeny bit:
  35.        if(*a==*b && (!strcmp(a,b)))
  36.  
  37. If you use a good compiler (like f.e. gcc) you should get an improvement
  38. better than the above by simply inline-ing a self-written strcmp function.
  39. Another thing oyu might consider and which would be useful for some
  40. types of machines like f.e. 680x0 , would be to do the first few cmp-s
  41. as byte-cmps until you get to a longword boundary, after which you do
  42. longword compares. When you have got long strings which often match in
  43. the first part (or you often compare matching strings) then this could
  44. give you a really remarkable preformance improvement.
  45. Maybe the best way to fix your problem would be to find our if you REALLY
  46. need that many strcmp-s. If you f.e. do some kind of parseing and simply
  47. check the text against a list of keywords there are MANY better solutions,
  48. from sort-ing the keyword list and using a binary search, to using a
  49. hash-function (there are programms which can help you create them), to
  50. using lex.
  51.  
  52.  
  53. Theorectial Physicist,N.:A physicist whose   | Send mail
  54. existence is postulated, to make the numbers |  to
  55. balance but who is never actually observed   | cedman@golem.ps.uci.edu
  56. in the laboratory.                           | edmanc@uciph0.ps.uci.edu
  57.