From: tomas@tuxteam.de
To: Daniel Colascione <daniel@censorshipresearch.org>
Cc: Ken Raeburn <raeburn@raeburn.org>,
Emacs development discussions <emacs-devel@gnu.org>
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 [thread overview]
Message-ID: <20100616174420.GA2847@tomas> (raw)
In-Reply-To: <4C01B609.6070303@censorshipresearch.org>
-----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 nreverse
> > obviously makes things a little faster, but are you doing more work each
> > time around the loop to maintain and use the tail pointer?)
>
> 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)))
|
| (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)))
|
| (defun runtwo (n)
| (let ((lst))
| (while (> n 0)
| (setq lst (cons n lst)
| n (1- n)))
| (garbage-collect)
| (cons (benchmark-run (copy1 lst))
| (benchmark-run (copy2 lst)))))
|
| (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ás
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)
iD8DBQFMGQ10Bcgs9XrR2kYRAoRdAJoCbSaPZ2eUX6QiKKDW1NjQGV3G8gCfca9C
tyyHzMbrUJopGOPzwTEJUjs=
=ZBiq
-----END PGP SIGNATURE-----
next prev parent reply other threads:[~2010-06-16 17:44 UTC|newest]
Thread overview: 21+ messages / expand[flat|nested] mbox.gz Atom feed top
2010-05-29 21:56 O(N^2) behavior in LOOP Daniel Colascione
2010-05-29 22:06 ` Daniel Colascione
2010-05-29 22:14 ` Lennart Borgman
2010-05-29 22:35 ` Geoff Gole
2010-05-29 23:58 ` [PATCH] use tail pointer for LOOP (Was: Re: O(N^2) behavior in LOOP) Daniel Colascione
2010-05-30 0:45 ` Ken Raeburn
2010-05-30 0:49 ` Daniel Colascione
2010-06-16 17:44 ` tomas [this message]
2010-06-16 18:10 ` [PATCH] use tail pointer for LOOP David Kastrup
2010-06-17 5:10 ` tomas
2010-06-17 7:18 ` Thien-Thi Nguyen
2010-06-17 9:22 ` tomas
2010-06-17 10:03 ` Thien-Thi Nguyen
2010-06-17 14:05 ` tomas
2010-06-17 15:16 ` Thien-Thi Nguyen
2010-06-17 10:12 ` Thien-Thi Nguyen
2010-06-17 20:48 ` Stefan Monnier
2010-06-18 7:07 ` David Kastrup
2010-06-18 13:40 ` Stefan Monnier
2010-05-30 17:05 ` Štěpán Němec
2010-05-30 17:09 ` Daniel Colascione
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20100616174420.GA2847@tomas \
--to=tomas@tuxteam.de \
--cc=daniel@censorshipresearch.org \
--cc=emacs-devel@gnu.org \
--cc=raeburn@raeburn.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.