From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Eli Zaretskii Newsgroups: gmane.emacs.help Subject: Re: How to count the number of occurrences of a character in a string? Date: Wed, 14 Oct 2015 20:06:41 +0300 Message-ID: <834mhtxtum.fsf@gnu.org> References: <874mhvq3b2.fsf@mbork.pl> <8337xezpdc.fsf@gnu.org> <83vbaay4dd.fsf@gnu.org> <87wpuqxzrx.fsf@ul.ie> <87mvvlix4r.fsf@gmail.com> NNTP-Posting-Host: plane.gmane.org X-Trace: ger.gmane.org 1444842519 17472 80.91.229.3 (14 Oct 2015 17:08:39 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Wed, 14 Oct 2015 17:08:39 +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 19:08:30 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 1ZmPXR-00007T-Mj for geh-help-gnu-emacs@m.gmane.org; Wed, 14 Oct 2015 19:08:29 +0200 Original-Received: from localhost ([::1]:43633 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZmPXR-00075T-3z for geh-help-gnu-emacs@m.gmane.org; Wed, 14 Oct 2015 13:08:29 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:47417) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZmPVw-0006t1-7I for help-gnu-emacs@gnu.org; Wed, 14 Oct 2015 13:06:56 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ZmPVj-0005Cq-EJ for help-gnu-emacs@gnu.org; Wed, 14 Oct 2015 13:06:56 -0400 Original-Received: from mtaout22.012.net.il ([80.179.55.172]:57110) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZmPVj-0005CU-6M for help-gnu-emacs@gnu.org; Wed, 14 Oct 2015 13:06:43 -0400 Original-Received: from conversion-daemon.a-mtaout22.012.net.il by a-mtaout22.012.net.il (HyperSendmail v2007.08) id <0NW700800ZB1BA00@a-mtaout22.012.net.il> for help-gnu-emacs@gnu.org; Wed, 14 Oct 2015 20:06:41 +0300 (IDT) Original-Received: from HOME-C4E4A596F7 ([84.94.185.246]) by a-mtaout22.012.net.il (HyperSendmail v2007.08) with ESMTPA id <0NW70082RZJ56E40@a-mtaout22.012.net.il> for help-gnu-emacs@gnu.org; Wed, 14 Oct 2015 20:06:41 +0300 (IDT) In-reply-to: <87mvvlix4r.fsf@gmail.com> X-012-Sender: halo1@inter.net.il X-detected-operating-system: by eggs.gnu.org: Solaris 10 X-Received-From: 80.179.55.172 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:107640 Archived-At: > From: Oleh Krehel > Date: Wed, 14 Oct 2015 12:04:52 +0200 > > (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. Why? There's nothing wrong with aref.