From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: IRIE Shinsuke Newsgroups: gmane.emacs.bugs Subject: bug#7174: 23.2; `last' can be made faster by using `length' Date: Fri, 08 Oct 2010 10:31:46 +0900 Message-ID: <874ocxbhbx.wl%irieshinsuke@yahoo.co.jp> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 (generated by SEMI 1.14.6 - "Maruoka") Content-Type: text/plain; charset=US-ASCII X-Trace: dough.gmane.org 1286502604 6666 80.91.229.12 (8 Oct 2010 01:50:04 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Fri, 8 Oct 2010 01:50:04 +0000 (UTC) To: 7174@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Fri Oct 08 03:50:03 2010 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1P425s-00059J-A1 for geb-bug-gnu-emacs@m.gmane.org; Fri, 08 Oct 2010 03:49:56 +0200 Original-Received: from localhost ([127.0.0.1]:45522 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1P425r-0007bx-9q for geb-bug-gnu-emacs@m.gmane.org; Thu, 07 Oct 2010 21:49:55 -0400 Original-Received: from [140.186.70.92] (port=37404 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1P425g-0007Xo-W1 for bug-gnu-emacs@gnu.org; Thu, 07 Oct 2010 21:49:46 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1P41y4-00040X-Pr for bug-gnu-emacs@gnu.org; Thu, 07 Oct 2010 21:41:53 -0400 Original-Received: from debbugs.gnu.org ([140.186.70.43]:52656) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1P41y4-00040S-OQ for bug-gnu-emacs@gnu.org; Thu, 07 Oct 2010 21:41:52 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.69) (envelope-from ) id 1P41md-0004h0-VF; Thu, 07 Oct 2010 21:30:04 -0400 X-Loop: help-debbugs@gnu.org Resent-From: IRIE Shinsuke Original-Sender: debbugs-submit-bounces@debbugs.gnu.org Resent-To: owner@debbugs.gnu.org Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Fri, 08 Oct 2010 01:30:03 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 7174 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: X-Debbugs-Original-To: bug-gnu-emacs@gnu.org Original-Received: via spool by submit@debbugs.gnu.org id=B.128650136718013 (code B ref -1); Fri, 08 Oct 2010 01:30:03 +0000 Original-Received: (at submit) by debbugs.gnu.org; 8 Oct 2010 01:29:27 +0000 Original-Received: from localhost ([127.0.0.1] helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.69) (envelope-from ) id 1P41m2-0004gU-MX for submit@debbugs.gnu.org; Thu, 07 Oct 2010 21:29:26 -0400 Original-Received: from eggs.gnu.org ([140.186.70.92]) by debbugs.gnu.org with esmtp (Exim 4.69) (envelope-from ) id 1P41m0-0004gP-JM for submit@debbugs.gnu.org; Thu, 07 Oct 2010 21:29:25 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1P41p8-0002zj-NI for submit@debbugs.gnu.org; Thu, 07 Oct 2010 21:32:39 -0400 Original-Received: from lists.gnu.org ([199.232.76.165]:46657) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1P41p8-0002zf-L5 for submit@debbugs.gnu.org; Thu, 07 Oct 2010 21:32:38 -0400 Original-Received: from [140.186.70.92] (port=34108 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1P41p7-0004Xs-DB for bug-gnu-emacs@gnu.org; Thu, 07 Oct 2010 21:32:38 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1P41p6-0002zP-8u for bug-gnu-emacs@gnu.org; Thu, 07 Oct 2010 21:32:37 -0400 Original-Received: from mail-pw0-f41.google.com ([209.85.160.41]:61730) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1P41p6-0002zE-1x for bug-gnu-emacs@gnu.org; Thu, 07 Oct 2010 21:32:36 -0400 Original-Received: by pwj6 with SMTP id 6so259014pwj.0 for ; Thu, 07 Oct 2010 18:32:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:sender:date:message-id:from :to:subject:user-agent:mime-version:content-type; bh=dC60P5M3U1w8idGYdfSDs/1K6m6rjVdfpHRctq5y8rg=; b=tW5CBo3nb5coOG1u+lHanbjaJnXB+IMPGOly/zpnwL4Tn1dnmp0EDpeojWCDQRbyV+ 96vLA8Inv6U0Ru2k79vnNhQB6qd6VTT+AwHNz3Y2n1LpGMKKP1P54bETvwLBAUVYF480 Hvasg6VOxM9OrzenwLZJp9MHZH7cxjbtXeUts= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=sender:date:message-id:from:to:subject:user-agent:mime-version :content-type; b=OBFEjSPdHKQ3CmJdVJRLrNo6nawqYOZw+dMH9U/TWzJbx51TD70Ftgd0hks7soUzKh DMpiTtoaWjbLFvrm2YzWvSqMXGR5iCunK8N5m/fEwgl4HrYu+kMvfzE7Q75TZpnh/gth qBPBgJJRCPtsh5N+QjAJYS4WC95Up0UP34rpY= Original-Received: by 10.114.46.14 with SMTP id t14mr1866973wat.198.1286501554770; Thu, 07 Oct 2010 18:32:34 -0700 (PDT) Original-Received: from piyo.gmail.com (EM114-51-27-176.pool.e-mobile.ne.jp [114.51.27.176]) by mx.google.com with ESMTPS id q6sm4398751waj.22.2010.10.07.18.32.28 (version=TLSv1/SSLv3 cipher=RC4-MD5); Thu, 07 Oct 2010 18:32:31 -0700 (PDT) User-Agent: Wanderlust/2.15.6 (Almost Unreal) SEMI/1.14.6 (Maruoka) FLIM/1.14.9 (=?UTF-8?Q?Goj=1B$?=(=?UTF-8?Q?D+W=1B?=(B) APEL/10.7 Emacs/23.2 (x86_64-pc-linux-gnu) MULE/6.0 (HANACHIRUSATO) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6, seldom 2.4 (older, 4) X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.11 Precedence: list Resent-Date: Thu, 07 Oct 2010 21:30:03 -0400 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 3) X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.bugs:40763 Archived-At: Recently I saw the implementation of `last' in subr.el and found that it counts the number of list elements on its own: (defun last (list &optional n) "Return the last link of LIST. Its car is the last element. If LIST is nil, return nil. If N is non-nil, return the Nth-to-last link of LIST. If N is bigger than the length of LIST, return LIST." (if n (let ((m 0) (p list)) (while (consp p) (setq m (1+ m) p (cdr p))) (if (<= n 0) p (if (< n m) (nthcdr (- m n) list) list))) (while (consp (cdr list)) (setq list (cdr list))) list)) So I modified the code to use `length' as follows, and confirmed it becomes much faster: (defun last (list &optional n) "Return the last link of LIST. Its car is the last element. If LIST is nil, return nil. If N is non-nil, return the Nth-to-last link of LIST. If N is bigger than the length of LIST, return LIST." (if n (and (> n 0) (let ((m (length list))) (if (< n m) (nthcdr (- m n) list) list))) (and list (nthcdr (1- (length list)) list)))) Furthermore, the code can be made much simpler (but slower than the above, in particular cases) as: (defun last (list &optional n) "Return the last link of LIST. Its car is the last element. If LIST is nil, return nil. If N is non-nil, return the Nth-to-last link of LIST. If N is bigger than the length of LIST, return LIST." (nthcdr (- (length list) (or n 1)) list)) Thanks. IRIE Shinsuke