all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Oleh Krehel <ohwoeowho@gmail.com>
To: help-gnu-emacs@gnu.org
Subject: Re: How to count the number of occurrences of a character in a string?
Date: Wed, 14 Oct 2015 12:04:52 +0200	[thread overview]
Message-ID: <87mvvlix4r.fsf@gmail.com> (raw)
In-Reply-To: <d85fraFrkf4U1@mid.individual.net> (Joost Kremers's message of "13 Oct 2015 22:44:58 GMT")

Hi Joost,

Joost Kremers <joost.m.kremers@gmail.com> writes:

> You should byte-compile them. Makes the difference between cl-count and
> the rest even bigger.
>  
> These are the results from only one run (each function called 100000
> times), but I get similar results when I repeat the test:
>
> ,----
> | cl-count                            0.697451
> | count-char-in-string-cl-count       0.743856
> | count-char-in-string-mapcar         2.196961
> | count-char-in-string-mapc           1.817378
> | count-char-in-string-cdr            1.669147
> | count-char-in-string-string-match   3.815134
> `----
>
> (I left out split-string as it produces the wrong result when the char
> being counted appears twice in a row.)
>
> The cl-count vs. count-char-in-string-cl-count are a direct call to
> cl-count vs. cl-count wrapped in a function. There is obviously some
> overhead to the additional function call, but even then cl-count wins
> hands down.
>
> Code is here:
>
> https://gist.github.com/joostkremers/0e07a35c85758a2fcb52

Thanks for the test cases. Here are their results on my machine:

cl-count                            0.821659
my-count                            0.378911
count-char-in-string-cl-count       0.853604
count-char-in-string-mapcar         1.367351
count-char-in-string-mapc           1.372528
count-char-in-string-cdr            1.123716
count-char-in-string-string-match   2.478769

And here's the code of my-count - it's just cl-count simplified to work
only on strings:

(defun my-count (char str)
  (let ((count 0)
        (end (length str))
        (i 0)
        x)
    (while (< i end)
      (setq x (aref str i))
      (if (eq char x)
          (setq count (1+ count)))
      (setq i (1+ i)))
    count))

While `my-count' is twice as fast, I'd still recommend to use
`cl-count', unless the code is very performance-critical.

    Oleh



  reply	other threads:[~2015-10-14 10:04 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-10-12 19:45 How to count the number of occurrences of a character in a string? Marcin Borkowski
2015-10-12 21:43 ` Kaushal Modi
2015-10-12 23:27   ` Nick Dokos
2015-10-13  1:18   ` Stefan Monnier
2015-10-13 16:06     ` Kaushal Modi
2015-10-13 16:31       ` Kaushal Modi
2015-10-13 16:48       ` Eli Zaretskii
2015-10-13 17:41         ` Eli Zaretskii
2015-10-13 17:43         ` Kaushal Modi
2015-10-13 19:07           ` Eli Zaretskii
2015-10-13 19:56             ` Kaushal Modi
2015-10-13 20:17               ` Eli Zaretskii
2015-10-13 20:41                 ` Kaushal Modi
     [not found]               ` <mailman.267.1444767491.7904.help-gnu-emacs@gnu.org>
2015-10-13 20:46                 ` Brendan Halpin
2015-10-13 21:07                   ` Kaushal Modi
     [not found]                   ` <mailman.273.1444770543.7904.help-gnu-emacs@gnu.org>
2015-10-13 22:44                     ` Joost Kremers
2015-10-14 10:04                       ` Oleh Krehel [this message]
2015-10-14 17:06                         ` Eli Zaretskii
2015-10-13 20:43           ` Michael Heerdegen
2015-10-13 20:59             ` Charles Curley
2015-10-14 18:19           ` Noam Postavsky
2015-10-14 20:00             ` Kaushal Modi
2015-10-13 22:00 ` Emanuel Berg
     [not found] <mailman.164.1444679125.7904.help-gnu-emacs@gnu.org>
2015-10-12 23:35 ` Joost Kremers

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=87mvvlix4r.fsf@gmail.com \
    --to=ohwoeowho@gmail.com \
    --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.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.