Path: utzoo!attcan!utgpu!cs.utexas.edu!usc!orion.oac.uci.edu!cedman From: cedman@lynx.ps.uci.edu (Carl Edman) Newsgroups: alt.sources Subject: Re: Fast strcmp() wanted. Message-ID: Date: 29 Sep 90 16:11:18 GMT References: <1646@cherry.edc.UUCP> <1990Sep27.151543.8025@ccs.carleton.ca> Organization: non serviam Lines: 38 Nntp-Posting-Host: lynx.ps.uci.edu In-reply-to: dfs@doe.carleton.ca's message of 27 Sep 90 15:15:43 GMT In article <1990Sep27.151543.8025@ccs.carleton.ca> dfs@doe.carleton.ca (David F. Skoll) writes: In article cedman@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@golem.ps.uci.edu in the laboratory. | edmanc@uciph0.ps.uci.edu