9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
From: "rob pike" <rob@plan9.bell-labs.com>
To: 9fans@cse.psu.edu
Subject: Re: [9fans] plan or side effect
Date: Thu,  7 Mar 2002 08:45:53 -0500	[thread overview]
Message-ID: <9d0beeb37aae80180882f07b494170f3@plan9.bell-labs.com> (raw)

> 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 <u.h>
#include <libc.h>

#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 <u.h>
#include <libc.h>

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;
	}
}



             reply	other threads:[~2002-03-07 13:45 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-03-07 13:45 rob pike [this message]
2002-03-07 15:47 ` AMSRL-CI-C
  -- strict thread matches above, loose matches on Subject: below --
2002-03-08 19:22 forsyth
2002-03-06 10:24 geoff
2002-03-07  9:56 ` Douglas A. Gwyn
2002-03-05  9:54 Fco.J.Ballesteros
2002-03-06  9:51 ` Thomas Bushnell, BSG
2002-03-06  9:52 ` Douglas A. Gwyn
2002-03-08  9:59   ` ozan s. yigit
2002-03-01 11:35 forsyth
2002-02-28 17:41 David Gordon Hogan
2002-03-01 10:02 ` Thomas Bushnell, BSG
2002-03-01 12:07   ` Boyd Roberts
2002-03-04 10:04     ` Thomas Bushnell, BSG
2002-03-04 17:11       ` Sean Quinlan
2002-03-04 18:23       ` ozan s yigit
2002-03-05  9:41         ` Thomas Bushnell, BSG
2002-03-05  9:56           ` Boyd Roberts
2002-03-05  9:43       ` Boyd Roberts
2002-03-08 17:30         ` Thomas Bushnell, BSG
2002-03-08 18:00           ` Dan Cross
2002-03-11 10:04             ` Ralph Corderoy
2002-03-11 10:04             ` Thomas Bushnell, BSG
2002-03-01 11:57 ` Boyd Roberts
2002-02-27 15:08 presotto
2002-02-27 15:27 ` Sean Quinlan
2002-02-28  9:58   ` Thomas Bushnell, BSG
2002-02-28 12:51     ` Ralph Corderoy
2002-02-28 16:57       ` Thomas Bushnell, BSG
2002-02-28 16:01     ` AMSRL-CI-CN
2002-02-28 16:52       ` Thomas Bushnell, BSG

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=9d0beeb37aae80180882f07b494170f3@plan9.bell-labs.com \
    --to=rob@plan9.bell-labs.com \
    --cc=9fans@cse.psu.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).