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 17:07:32 -0400 Message-ID: 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; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Trace: ger.gmane.org 1444770573 7153 80.91.229.3 (13 Oct 2015 21:09:33 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Tue, 13 Oct 2015 21:09:33 +0000 (UTC) Cc: Help Gnu Emacs mailing list To: brendan.halpin@ul.ie Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Tue Oct 13 23:09:18 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 1Zm6ot-0008Ao-Mm for geh-help-gnu-emacs@m.gmane.org; Tue, 13 Oct 2015 23:09:15 +0200 Original-Received: from localhost ([::1]:39352 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Zm6ot-00069Q-1n for geh-help-gnu-emacs@m.gmane.org; Tue, 13 Oct 2015 17:09:15 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:39007) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Zm6nu-0005bE-K3 for help-gnu-emacs@gnu.org; Tue, 13 Oct 2015 17:08:15 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Zm6nt-0007qF-D4 for help-gnu-emacs@gnu.org; Tue, 13 Oct 2015 17:08:14 -0400 Original-Received: from mail-oi0-x22b.google.com ([2607:f8b0:4003:c06::22b]:34280) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Zm6nt-0007pu-2Z for help-gnu-emacs@gnu.org; Tue, 13 Oct 2015 17:08:13 -0400 Original-Received: by oiak8 with SMTP id k8so17273609oia.1 for ; Tue, 13 Oct 2015 14:08:12 -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=8kox6IalA5Goz6uSR9FVFHufsIUDzE7k+yCkB5tsZNI=; b=o9C+Fr6cQHJMWZcqGgID562SGZYvU4iCq73urREDWdNyO74RNpijGrwyolmRBC2Qex fwJAINbKFUJ4loZpYEU4oA3waN0x9AtHN7z7pg9cS6bcl++jNfQmH5sQ7klaljNwq+Zx EhrxyNP6uSqIE+tePwQM754h5AtRhaymOkR1X9cYmHwUWaw9JQxupalnzQ9eKKtAfvLL s/NqMYCw/d8ZH0ld5Cvv6u8oQGQPbfsDK6pbPZz/stFXow8Yjeq+Witm3HrPA32lQzvF 8d47VG8t62Lvo7SR6PoewH3YbV3GvpSqSs5PxAxUE1I5MzKj9yaw9vVijSXOyt0BP/i+ ONYg== X-Received: by 10.202.79.139 with SMTP id d133mr5606124oib.134.1444770492332; Tue, 13 Oct 2015 14:08:12 -0700 (PDT) Original-Received: by 10.202.172.205 with HTTP; Tue, 13 Oct 2015 14:07:32 -0700 (PDT) In-Reply-To: <87wpuqxzrx.fsf@ul.ie> X-detected-operating-system: by eggs.gnu.org: Error: Malformed IPv6 address (bad octet value). X-Received-From: 2607:f8b0:4003:c06::22b X-Content-Filtered-By: Mailman/MimeDel 2.1.14 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:107626 Archived-At: Looks like mapcar is =E2=80=8Bnow at =E2=80=8B the 2nd place by a hair.. mapc drops down to 3rd place =E2=80=8B. Updated https://gist.github.com/kaushalmodi/ab487f63727381179f61= =E2=80=8B =E2=80=8B ;; Fastest to slowest approach;;;; |------+--------------+-----------------------|;; | Rank | Method | Time in second |;; | | | for 100000 executions |;; | | | (very rough avg) |;; |------+--------------+-----------------------|;; | 1 | cl-count | 1.0 to 1.6 |;; | 2 | mapcar | 1.9 to 2.1 |;; | 3 | mapc | 2.0 to 2.6 |;; | 4 | cdr | 2.4 to 3.3 |;; | 5 | split-string | 4.0 to 4.7 |;; | 5* | string-match | 3.7 to 4.7 |;; |------+--------------+-----------------------| =E2=80=8B =E2=80=8B -- Kaushal Modi On Tue, Oct 13, 2015 at 4:46 PM, Brendan Halpin wrote: > On Tue, Oct 13 2015, Eli Zaretskii wrote: > > > Also, did you actually try 'mapc', per my second suggestion? > > Why not mapcar? > > (defun test (str char) > (apply '+ (mapcar (lambda (x) (if (=3D x char) 1 0)) > str))) > > B > -- > Brendan Halpin, Head, Department of Sociology, University of Limerick, > Ireland > Tel: w +353-61-213147 f +353-61-202569 h +353-61-338562; Room F1-002 x > 3147 > mailto:brendan.halpin@ul.ie ULSociology on Facebook: > http://on.fb.me/fjIK9t > http://teaching.sociology.ul.ie/bhalpin/wordpress > twitter:@ULSociology >