From mboxrd@z Thu Jan 1 00:00:00 1970 Message-ID: <9d0beeb37aae80180882f07b494170f3@plan9.bell-labs.com> To: 9fans@cse.psu.edu Subject: Re: [9fans] plan or side effect From: "rob pike" MIME-Version: 1.0 Content-Type: text/plain; charset="US-ASCII" Content-Transfer-Encoding: 7bit Date: Thu, 7 Mar 2002 08:45:53 -0500 Topicbox-Message-UUID: 612b39f8-eaca-11e9-9e20-41e7f4b1d025 > Note that under reasonable assumptions the macro saves at least a > dozen function calls for each redundant test of byte equality, so > the more conservative version is still a big performance win. Function calls are cheap. They used to be expensive, but they're really not any more. I tried the attached program and got a factor of about 2.5 between the two programs (mips or x86). Given that our strcmp is not clever, one might have expected a bigger difference. No denying the macro makes a difference, but given their dangers, you'd have to have a pretty special program before this change could make a worthwhile improvement. I tried inlining (by hand) strcmp - something gcc does just fine - and got exactly the same speedup. Surely inlining is safer than this sort of hack, if you can inline. -rob #include #include #define StrEq(a,b) (*(a)==*(b) && strcmp((a),(b))==0) void main(int argc, char *argv[]) { char *a, *b; int i, j; a = "asdfadfdsf"; b = "bsdfzxcvvx"; for(i=0; i<1000*1000*100; i++) // if(StrEq(a,b)) if(strcmp(a,b)==0) j++; } our strcmp: #include #include int strcmp(char *s1, char *s2) { unsigned c1, c2; for(;;) { c1 = *s1++; c2 = *s2++; if(c1 != c2) { if(c1 > c2) return 1; return -1; } if(c1 == 0) return 0; } }