From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Oleh Krehel Newsgroups: gmane.emacs.help Subject: Re: How to count the number of occurrences of a character in a string? Date: Wed, 14 Oct 2015 12:04:52 +0200 Message-ID: <87mvvlix4r.fsf@gmail.com> References: <874mhvq3b2.fsf@mbork.pl> <8337xezpdc.fsf@gnu.org> <83vbaay4dd.fsf@gnu.org> <87wpuqxzrx.fsf@ul.ie> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: ger.gmane.org 1444817232 28376 80.91.229.3 (14 Oct 2015 10:07:12 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Wed, 14 Oct 2015 10:07:12 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Wed Oct 14 12:07:06 2015 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1ZmIxd-00020V-JC for geh-help-gnu-emacs@m.gmane.org; Wed, 14 Oct 2015 12:07:05 +0200 Original-Received: from localhost ([::1]:41420 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZmIxd-00024D-3T for geh-help-gnu-emacs@m.gmane.org; Wed, 14 Oct 2015 06:07:05 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:57619) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZmIvQ-0001x8-Ij for help-gnu-emacs@gnu.org; Wed, 14 Oct 2015 06:04:49 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ZmIvJ-00076Z-1v for help-gnu-emacs@gnu.org; Wed, 14 Oct 2015 06:04:45 -0400 Original-Received: from mail-wi0-x22d.google.com ([2a00:1450:400c:c05::22d]:33438) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZmIvI-00076J-Rk for help-gnu-emacs@gnu.org; Wed, 14 Oct 2015 06:04:40 -0400 Original-Received: by wicge5 with SMTP id ge5so93503294wic.0 for ; Wed, 14 Oct 2015 03:04:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:subject:references:date:in-reply-to:message-id:user-agent :mime-version:content-type; bh=gWPDiD3MhiUG+TNQupft7C6ey+s/uXQTLmu3VCjyNQs=; b=LjflAw7Q5ydMUxYJ7n7GRcDFuCY83wVOyZwr78yhkz+8F9xcJGbpr1fshV2ai+LehR gfYXxQm1VmmQLpQhr/P+nPiYP8lqhA6gRL5QLiNZeFP6zLacg3IgodJoErvbsYRlfdqA PB9rDjsWgJxoBOJ175lG3vaL0T2gMJfnHSSUYxb1diZZz91b1WMZWvRDSIggD++Kf4AA s4rmr2sOJHAFcM+RR9qpx1audSaT0ZtQ4QWf7mucZYR7UwsL3GOP1EZtXqKXKTBOV+z9 AefmDulVmxyru6cEdZQ6dHOfbDa82QdpPRl+ANgd75bqJUvW9YhzxepItcDiEAgz5b7I 3prQ== X-Received: by 10.180.36.212 with SMTP id s20mr3449674wij.14.1444817080087; Wed, 14 Oct 2015 03:04:40 -0700 (PDT) Original-Received: from firefly (dyn069045.nbw.tue.nl. [131.155.69.45]) by smtp.gmail.com with ESMTPSA id ry1sm8986728wjb.24.2015.10.14.03.04.39 for (version=TLSv1.2 cipher=RC4-SHA bits=128/128); Wed, 14 Oct 2015 03:04:39 -0700 (PDT) In-Reply-To: (Joost Kremers's message of "13 Oct 2015 22:44:58 GMT") User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.0.50 (gnu/linux) X-detected-operating-system: by eggs.gnu.org: Error: Malformed IPv6 address (bad octet value). X-Received-From: 2a00:1450:400c:c05::22d X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:107634 Archived-At: Hi Joost, Joost Kremers 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