;; 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)