From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: tomas@tuxteam.de Newsgroups: gmane.emacs.help Subject: Re: `compare-strings' style question Date: Tue, 24 Nov 2009 10:32:04 +0100 Message-ID: <20091124093204.GA1197@tomas> References: <20091119105024.GA6950@tomas> <20091120070318.GD30194@tomas> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; x-action=pgp-signed Content-Transfer-Encoding: quoted-printable X-Trace: ger.gmane.org 1259055581 26777 80.91.229.12 (24 Nov 2009 09:39:41 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Tue, 24 Nov 2009 09:39:41 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Tue Nov 24 10:39:34 2009 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.50) id 1NCrrt-0007o1-Nn for geh-help-gnu-emacs@m.gmane.org; Tue, 24 Nov 2009 10:39:31 +0100 Original-Received: from localhost ([127.0.0.1]:36108 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1NCrrt-0006iO-2T for geh-help-gnu-emacs@m.gmane.org; Tue, 24 Nov 2009 04:39:29 -0500 Original-Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1NCrrU-0006hr-F0 for help-gnu-emacs@gnu.org; Tue, 24 Nov 2009 04:39:04 -0500 Original-Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1NCrrO-0006gu-OM for help-gnu-emacs@gnu.org; Tue, 24 Nov 2009 04:39:03 -0500 Original-Received: from [199.232.76.173] (port=49666 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1NCrrO-0006gm-HH for help-gnu-emacs@gnu.org; Tue, 24 Nov 2009 04:38:58 -0500 Original-Received: from alextrapp1.equinoxe.de ([217.22.192.104]:33287 helo=www.elogos.de) by monty-python.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1NCrrO-0005Zx-5c for help-gnu-emacs@gnu.org; Tue, 24 Nov 2009 04:38:58 -0500 Original-Received: by www.elogos.de (Postfix, from userid 1000) id A5FFA90048; Tue, 24 Nov 2009 10:32:04 +0100 (CET) Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.15+20070412 (2007-04-11) X-detected-operating-system: by monty-python.gnu.org: GNU/Linux 2.6 (newer, 2) X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:70079 Archived-At: -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On Fri, Nov 20, 2009 at 03:34:51AM -0700, Kevin Rodgers wrote: [...] > (when (let ((case-fold-search nil)) > (string-match (concat "^" (regexp-quote foo)) bar)) > ...) > >> Thanks -- but I was trying to avoid conjuring up the whole regexp >> machinery for this task. I admit that this looks less confusing, thoug= h. > > Perhaps you could enlighten us with some performance measurements? Originally it was more of an "economy of the tools" principle and not real concern about (computer) performance. But your question piqued my curiosity, so here is a firstt shot at that: (let ((words '("Carl" "Carl's" "Carla" "Carla's" "Carlene" "Carlene's" "Carli= n" "Carlin's" "Carlo" "Carlo's" "Carlos" "Carlsbad" "Carlsbad's" "Carlson" "Carlson's" "Carlton" "Carlton's" "Carly" "Carly's" "Carlyle" "Carlyle's" "Carmela" "Carmela's" "Carmella" "Carmel= la's" "Carmelo" "Carmelo's" "Carmen" "Carmen's" "Carmichael" "Carmic= hael's" "Carmine" "Carmine's" "Carnap" "Carnap's" "Carnation" "Carnati= on's" "Carnegie" "Carnegie's" "Carney" "Carney's" "Carnot" "Carnot's= " "Carol" "Carol's" "Carole" "Carole's" "Carolina"))) (insert (format "compare-strings: %S\n" (benchmark-run-compiled 10000 (mapc (lambda (w) (compare-strings "Carm" 0 3 w 0 3)) words)))) (insert (format "string-match : %S\n" (benchmark-run-compiled 10000 (mapc (lambda (w) (string-match "^Carm" w)) words))))) compare-strings: (0.399947 0 0.0) string-match : (0.885371 0 0.0) compare-strings: (0.387 0 0.0) string-match : (0.870512 0 0.0) compare-strings: (0.35596 0 0.0) string-match : (0.88489 0 0.0) This is with "benchmark-run" instead of "benchmark-run-compiled": compare-strings: (0.61102 1 0.038892999999999955) string-match : (0.980834 1 0.038853999999999944) compare-strings: (0.6046680000000001 1 0.03880600000000001) string-match : (1.002827 1 0.03884599999999999) compare-strings: (0.608943 1 0.039271) string-match : (0.979522 1 0.03894399999999998) Thus, compare-strings seems a tad faster, although I don't believe it does matter very much (bear in mind *I* rigged the benchmark, tho ;-) I don't know how Emacs handles its regular expressions (whether it caches the compiled regexp and on which occassions it invalidates its cache), but possibly your idiom above (string-match (concat "^" ...)) will kill another bunch of CPU cycles. But as I said, peerformance wasn't my primary concern. Heck. Let's try. Doing just (concat "^" "Carm") instead of "^Carm" compiled: compare-strings: (0.400415 0 0.0) string-match : (0.891014 0 0.0) non-compiled: compare-strings: (0.6066699999999999 1 0.04038800000000009) string-match : (2.790288 35 1.410207000000001) Is it the concat? Is it the re-compiling of the regexp? Dunno. > Another contender: > > (let ((foo-len (length foo)) > (bar-len (length bar))) > (cond ((> bar-len foo-len) > (equal foo (substring bar 0 (1- foo-len)))) > ((=3D bar-len foo-len) > (equal foo bar)) > (t nil))) This doesn't make the code much more readable, I fear. Thanks - -- tom=C3=A1s -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.6 (GNU/Linux) iD8DBQFLC6gUBcgs9XrR2kYRAiWgAJ9gUWenOgH6YiVlgrDY4eW2VOrQ0ACfXPK+ wmS8QK1x3CregqNlZa1/eW4=3D =3Df7vF -----END PGP SIGNATURE-----