From: tomas@tuxteam.de
To: Thien-Thi Nguyen <ttn@gnuvola.org>
Cc: tomas@tuxteam.de, emacs-devel@gnu.org
Subject: Re: [PATCH] use tail pointer for LOOP
Date: Thu, 17 Jun 2010 11:22:32 +0200 [thread overview]
Message-ID: <20100617092232.GA27846@tomas> (raw)
In-Reply-To: <87mxuu5dq3.fsf@ambire.localdomain>
[-- Attachment #1.1: Type: text/plain, Size: 943 bytes --]
On Thu, Jun 17, 2010 at 09:18:28AM +0200, Thien-Thi Nguyen wrote:
> () tomas@tuxteam.de
> () Thu, 17 Jun 2010 07:10:21 +0200
>
> Still, reversing seems to be worth it (by some 30 percent).
> Unless we find some way to streamline the tail pointer better.
>
> How does this variant fare?
>
> (defun copy3 (lst)
> "Return a copy of LST."
> (let* ((box (list nil))
> (tp box))
> (while lst
> (setq tp (cdr (nconc tp (list (pop lst))))))
> (cdr box)))
Cute. You all are successfully keeping me from work. Enjoing it ;-)
Here are the results. Attached the modified source.
copy1: (1.058881 5 0.7366780000000048)
copy2: (1.27958 6 0.8913360000000026)
copy3: (1.337353 6 0.9249420000000015)
Still the reverse version is the winner. Yours seems to be a tad slower
(although I wouldn't know whether it's in the noise).
But it looks so sharp ;-)
Thanks, regards
-- tomás
[-- Attachment #1.2: bm.el --]
[-- Type: text/plain, Size: 1403 bytes --]
;; This buffer is for notes you don't want to save, and for Lisp evaluation.
;; If you want to create a file, visit that file with C-x C-f,
;; then enter the text in that file's own buffer.
(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 copy3 (lst)
"Return a copy of LST."
(let* ((box (list nil))
(tp box))
(while lst
(setq tp (cdr (nconc tp (list (pop lst))))))
(cdr box)))
(defun do-benchmark (n)
(let ((lst))
(while (> n 0)
(setq lst (cons n lst)
n (1- n)))
(garbage-collect)
(message (concat "copy1: %S\n"
"copy2: %S\n"
"copy3: %S\n")
(benchmark-run (copy1 lst))
(benchmark-run (copy2 lst))
(benchmark-run (copy3 lst)))))
(byte-compile 'copy1)
(byte-compile 'copy2)
(byte-compile 'copy3)
(byte-compile 'do-benchmark)
(do-benchmark 1000000)
[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 189 bytes --]
next prev parent reply other threads:[~2010-06-17 9:22 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
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 [this message]
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
List information: https://www.gnu.org/software/emacs/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20100617092232.GA27846@tomas \
--to=tomas@tuxteam.de \
--cc=emacs-devel@gnu.org \
--cc=ttn@gnuvola.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 public inbox
https://git.savannah.gnu.org/cgit/emacs.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).