unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: tomas@tuxteam.de
To: help-gnu-emacs@gnu.org
Subject: Re: `compare-strings' style question
Date: Tue, 24 Nov 2009 10:32:04 +0100	[thread overview]
Message-ID: <20091124093204.GA1197@tomas> (raw)
In-Reply-To: <he5rc4$3a3$1@ger.gmane.org>

-----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, though.
>
> 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" "Carlin"
           "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" "Carmella's"
           "Carmelo" "Carmelo's" "Carmen" "Carmen's" "Carmichael" "Carmichael's"
           "Carmine" "Carmine's" "Carnap" "Carnap's" "Carnation" "Carnation'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))))
> 	((= bar-len foo-len)
> 	 (equal foo bar))
> 	(t nil)))

This doesn't make the code much more readable, I fear.

Thanks
- -- tomás
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)

iD8DBQFLC6gUBcgs9XrR2kYRAiWgAJ9gUWenOgH6YiVlgrDY4eW2VOrQ0ACfXPK+
wmS8QK1x3CregqNlZa1/eW4=
=f7vF
-----END PGP SIGNATURE-----




  reply	other threads:[~2009-11-24  9:32 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-11-19 10:50 `compare-strings' style question tomas
2009-11-20  3:08 ` Kevin Rodgers
2009-11-20  7:03   ` tomas
2009-11-20 10:34     ` Kevin Rodgers
2009-11-24  9:32       ` tomas [this message]
     [not found] <mailman.11037.1258628274.2239.help-gnu-emacs@gnu.org>
2009-11-19 11:39 ` David Kastrup
2009-11-19 15:27   ` tomas
2009-11-19 19:55     ` Andreas Politz
2009-11-20  6:47       ` tomas
     [not found]   ` <mailman.11057.1258644880.2239.help-gnu-emacs@gnu.org>
2009-11-19 16:17     ` David Kastrup
2009-11-19 20:54       ` Barry Margolin
2009-11-20  7:00         ` tomas
2009-11-20  8:13           ` Andreas Politz
2009-11-20  6:53       ` tomas

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

  List information: https://www.gnu.org/software/emacs/

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

  git send-email \
    --in-reply-to=20091124093204.GA1197@tomas \
    --to=tomas@tuxteam.de \
    --cc=help-gnu-emacs@gnu.org \
    /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 read-only IMAP folder(s) and NNTP newsgroup(s).