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

  1. From: cedman@lynx.ps.uci.edu (Carl Edman)
  2. Newsgroups: alt.sources
  3. Subject: Re: Fast strcmp() wanted.
  4. Message-ID: <CEDMAN.90Sep29091115@lynx.ps.uci.edu>
  5. Date: 29 Sep 90 16:11:18 GMT
  6.     <CEDMAN.90Sep27075013@lynx.ps.uci.edu>
  7.     <1990Sep27.151543.8025@ccs.carleton.ca>
  8. Organization: non serviam
  9. Lines: 38
  10. Nntp-Posting-Host: lynx.ps.uci.edu
  11. In-reply-to: dfs@doe.carleton.ca's message of 27 Sep 90 15:15:43 GMT
  12.  
  13. In article <1990Sep27.151543.8025@ccs.carleton.ca> dfs@doe.carleton.ca (David F. Skoll) writes:
  14.    In article <CEDMAN.90Sep27075013@lynx.ps.uci.edu>
  15.    cedman@lynx.ps.uci.edu (Carl Edman) writes:
  16.  
  17.    >      One quick dirty thing I did once was to change
  18.    >          if (strcmp (a,b)==0)
  19.    >      to
  20.    >          if (*a==*b && (strcmp(a,b)==0))
  21.    >
  22.    >      I seem to remember a remarkable performance improvement, like about 5
  23.    >      times faster.  Probably due to the fact that the program mainly did
  24.    >      strcmp and the strcmp was pretty bad.
  25.  
  26.    Hmm... that seems strange.  If the first characters of the strings differ,
  27.    most strcmps will not bother to test the rest.  All that the above code
  28.    possibly saves you is a function call/return.  If this makes such a huge
  29.    difference, then the compiler or your machine's architecture must be pretty
  30.    bad.
  31.  
  32. I did NOT write the above original article ! Please get your attributions
  33. right. All I wrote was one response to it, which is not quoted here.
  34.  
  35. As to the content: Yes, all that saves is the function call overhead
  36. but that can be quite a substantial amount even on machines with good
  37. compilers. That is why I suggested inline-ing and (under some circumstances)
  38. a rewritten strcmp which uses longword compares. Another possibility which
  39. comes to mind when every string is compared very often, is to create a
  40. string structure (or better class, long live C++ ! :-) which calculates
  41. a 32-bit CRC for each string the first time and stores it somewhere.
  42. Then only 1 (inlined) longword-compare will do the stringcomparisons
  43. for you.
  44.  
  45.  
  46.  
  47. Theorectial Physicist,N.:A physicist whose   | Send mail
  48. existence is postulated, to make the numbers |  to
  49. balance but who is never actually observed   | cedman@golem.ps.uci.edu
  50. in the laboratory.                           | edmanc@uciph0.ps.uci.edu
  51.