From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: tomas@tuxteam.de Newsgroups: gmane.emacs.devel Subject: Re: [PATCH] use tail pointer for LOOP (Was: Re: O(N^2) behavior in LOOP) Date: Wed, 16 Jun 2010 19:44:20 +0200 Message-ID: <20100616174420.GA2847@tomas> References: <4C018D79.7040409@censorshipresearch.org> <4C018FD3.1020305@censorshipresearch.org> <4C01AA28.6030002@censorshipresearch.org> <9718A5AD-7A74-470B-A32D-DA14266506A3@raeburn.org> <4C01B609.6070303@censorshipresearch.org> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; x-action=pgp-signed Content-Transfer-Encoding: quoted-printable X-Trace: dough.gmane.org 1276711468 18366 80.91.229.12 (16 Jun 2010 18:04:28 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Wed, 16 Jun 2010 18:04:28 +0000 (UTC) Cc: Ken Raeburn , Emacs development discussions To: Daniel Colascione Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Wed Jun 16 20:04:25 2010 connect(): No such file or directory Return-path: Envelope-to: ged-emacs-devel@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 1OOwyO-0000R0-RE for ged-emacs-devel@m.gmane.org; Wed, 16 Jun 2010 20:04:25 +0200 Original-Received: from localhost ([127.0.0.1]:34185 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1OOwko-0006CS-Me for ged-emacs-devel@m.gmane.org; Wed, 16 Jun 2010 13:50:22 -0400 Original-Received: from [140.186.70.92] (port=55062 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1OOwek-0003LJ-Hr for emacs-devel@gnu.org; Wed, 16 Jun 2010 13:44:16 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.69) (envelope-from ) id 1OOwef-0001tk-M3 for emacs-devel@gnu.org; Wed, 16 Jun 2010 13:44:02 -0400 Original-Received: from alextrapp1.equinoxe.de ([217.22.192.104]:42947 helo=www.elogos.de) by eggs.gnu.org with esmtp (Exim 4.69) (envelope-from ) id 1OOwef-0001rn-B9 for emacs-devel@gnu.org; Wed, 16 Jun 2010 13:44:01 -0400 Original-Received: by www.elogos.de (Postfix, from userid 1000) id 5DD3A9004D; Wed, 16 Jun 2010 19:44:20 +0200 (CEST) Content-Disposition: inline In-Reply-To: <4C01B609.6070303@censorshipresearch.org> User-Agent: Mutt/1.5.15+20070412 (2007-04-11) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2) X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.devel:126026 Archived-At: -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On Sat, May 29, 2010 at 08:49:13PM -0400, Daniel Colascione wrote: > On 5/29/10 8:45 PM, Ken Raeburn wrote: [...] > > Is it any faster to build the list in order? (Simply avoiding nrevers= e > > obviously makes things a little faster, but are you doing more work e= ach > > time around the loop to maintain and use the tail pointer?) >=20 > It's only a little bit more work to use the tail pointer [...] This has intrigued me for quite a while. Since I really should be doing tax declarations, I couldn't hold back for longer -- here is my (very unscientific) approach, to help you all procrastinate a bit too: | (defun copy1 (lst) | "Build up a copy of lst by consing up in reverse order, then | reversing" | (let ((res)) | (while lst | (setq res (cons (car lst) res) | lst (cdr lst))) | (nreverse res))) |=20 | (defun copy2 (lst) | "Build up a copy of lst by consing up in order, keeping a tail | pointer" | (when lst | (let ((res) (tail)) | (setq res (cons (car lst) nil) | tail res | lst (cdr lst)) | (while lst | (setcdr tail (cons (car lst) nil)) | (setq tail (cdr tail) | lst (cdr lst))) | res))) |=20 | (defun runtwo (n) | (let ((lst)) | (while (> n 0) | (setq lst (cons n lst) | n (1- n))) | (garbage-collect) | (cons (benchmark-run (copy1 lst))=20 | (benchmark-run (copy2 lst))))) |=20 | (runtwo 1000000) (Maybe the tail pointer version could be done more elegantly: I'd be delighted to be taught more :) Turns out that the nreverse version is a tad faster (on my hardware, one of those Atom based netbooks, in case it matters) -- about 2.1 versus 2.7 seconds for a list of size 10^6. Garbage collections are comparable. For the very curious (and to add some scientific varnish to this ;-), here's my Emacs: GNU Emacs 23.1.91.1 (i486-pc-linux-gnu, GTK+ Version 2.12.12) of 2010-01-11 on elegiac, modified by Debian Enjoy -- and may this keep you too from doing more important things ;-) Regards - -- tom=C3=A1s -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.6 (GNU/Linux) iD8DBQFMGQ10Bcgs9XrR2kYRAoRdAJoCbSaPZ2eUX6QiKKDW1NjQGV3G8gCfca9C tyyHzMbrUJopGOPzwTEJUjs=3D =3DZBiq -----END PGP SIGNATURE-----