From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Kaushal Modi Newsgroups: gmane.emacs.help Subject: Re: How to count the number of occurrences of a character in a string? Date: Tue, 13 Oct 2015 13:43:05 -0400 Message-ID: References: <874mhvq3b2.fsf@mbork.pl> <8337xezpdc.fsf@gnu.org> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: ger.gmane.org 1444758297 11989 80.91.229.3 (13 Oct 2015 17:44:57 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Tue, 13 Oct 2015 17:44:57 +0000 (UTC) Cc: Help Gnu Emacs mailing list To: Eli Zaretskii Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Tue Oct 13 19:44:56 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 1Zm3d9-00049E-Ia for geh-help-gnu-emacs@m.gmane.org; Tue, 13 Oct 2015 19:44:55 +0200 Original-Received: from localhost ([::1]:38423 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Zm3d8-0005eM-Rf for geh-help-gnu-emacs@m.gmane.org; Tue, 13 Oct 2015 13:44:54 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:41164) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Zm3c7-0005XB-Ir for help-gnu-emacs@gnu.org; Tue, 13 Oct 2015 13:43:52 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Zm3c3-0003cB-Ik for help-gnu-emacs@gnu.org; Tue, 13 Oct 2015 13:43:51 -0400 Original-Received: from mail-oi0-x229.google.com ([2607:f8b0:4003:c06::229]:35658) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Zm3c1-0003bh-Re; Tue, 13 Oct 2015 13:43:45 -0400 Original-Received: by oiao187 with SMTP id o187so13968133oia.2; Tue, 13 Oct 2015 10:43:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type; bh=layWdfYDOo8xeb7VUpo5c4FNndjsdJJmvJY9qV8ohAE=; b=gO9tHRTQvrRcCX14pKEU/8VHgTBvdoSZ12cJjB9RW5XMqvla/xocWU9mspdxAZVHDZ BbXeUpespBcP3n9hKwd31hXfqhKGlN2XO8KBZTlo09KyT75QppogvysxLX53bnBsTMOy tHoUek1C2ok7hPeW9V5WNy71+kR1NMPecXRVgrk9aEwVpv0F7KIutFKKZwKRdks5zjls VfMHgFKiN1P8gQitcGuS7VNNhcxZKE+c9P7nmloDESHX5QFv42eY3yZ9I8JxFTXApmm2 sZeOWySbMmO7QvuwPqxeMKj4FBLXbJLaCbQR+JlzB3EasoUtlZ7Q8TLSdEci8XQweFHu 0KCQ== X-Received: by 10.202.173.77 with SMTP id w74mr19382949oie.128.1444758225311; Tue, 13 Oct 2015 10:43:45 -0700 (PDT) Original-Received: by 10.202.172.205 with HTTP; Tue, 13 Oct 2015 10:43:05 -0700 (PDT) In-Reply-To: <8337xezpdc.fsf@gnu.org> X-detected-operating-system: by eggs.gnu.org: Error: Malformed IPv6 address (bad octet value). X-Received-From: 2607:f8b0:4003:c06::229 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:107612 Archived-At: Thanks Eli. That's neat! I was over-engineering :) I am, though, unable to quantify which approach is the fastest using `benchmark`. I ran the below wrapper progn block couple of times and I got different results each time. Oddly enough, I even got negative execution times. What am I doing wrong? RESULT: 100000 executions of string-match-p approach: Elapsed time: 0.000330s 100000 executions of cl-count approach: Elapsed time: 0.000357s 100000 executions of cdr approach: Elapsed time: -0.000378s <<<<<<<<<<<<<<<<<<< negative time?? nil 100000 executions of string-match-p approach: Elapsed time: 0.001972s 100000 executions of cl-count approach: Elapsed time: 0.000728s 100000 executions of cdr approach: Elapsed time: 0.000638s nil 100000 executions of string-match-p approach: Elapsed time: -0.000199s <<<<<<<<<<<<<<<<<<< negative time?? 100000 executions of cl-count approach: Elapsed time: 0.000766s 100000 executions of cdr approach: Elapsed time: 0.001562s nil CODE: (progn (message "100000 executions of string-match-p approach: ") (benchmark 100000 (progn (defun my/count-char-in-string (char str) "Count the number of times CHAR character appears in STR string." ;; (message "\n==========\nstr = %0s" str) (let* ((num-matches 0) (ptr 0) ; initiate pointer for string match match-pos) (while (<= ptr (length str)) ;; (message "ptr = %0d" ptr) (setq match-pos (string-match-p (regexp-quote (char-to-string char)) str ptr)) (if match-pos (progn (setq ptr (1+ match-pos)) ;; (message "match-pos = %0d ptr = %0d" match-pos ptr) (setq num-matches (1+ num-matches))) (progn (setq ptr (1+ (length str)))))) ;; (message "%0d occurrence%0s of `%c' char found in \"%s\"." ;; num-matches (if (/= 1 num-matches) "s" "") char str) num-matches)) (my/count-char-in-string ?a "abcda") (my/count-char-in-string ?z "abcda") (my/count-char-in-string ?f "falalala") (my/count-char-in-string ?l "falalalaabcds") (my/count-char-in-string ?^ "f^la^lala^dabra^^") (my/count-char-in-string ?\\ "\\falalala\\"))) (message "100000 executions of cl-count approach: ") (benchmark 100000 (progn (cl-count ?a "abcda") (cl-count ?z "abcda") (cl-count ?f "falalala") (cl-count ?l "falalalaabcds") (cl-count ?^ "f^la^lala^dabra^^") (cl-count ?\\ "\\falalala\\"))) (message "100000 executions of cdr approach: ") (benchmark 100000 (progn (defun my2/count-char-in-string (char str) (let ((str-list (append str nil)) (num-matches 0)) (while str-list (if (= (car str-list) char) (setq num-matches (1+ num-matches))) (setq str-list (cdr str-list))) num-matches)) (my2/count-char-in-string ?a "abcda") (my2/count-char-in-string ?z "abcda") (my2/count-char-in-string ?f "falalala") (my2/count-char-in-string ?l "falalalaabcds") (my2/count-char-in-string ?^ "f^la^lala^dabra^^") (my2/count-char-in-string ?\\ "\\falalala\\"))) nil) -- Kaushal Modi