[comp.lang.lisp] Loop speeds II

wilk@fife.cs.cornell.edu (Michael Wilk) (10/16/90)

In article <EB.90Oct15142403@sunvalleymall.lucid> eb@sunvalleymall.lucid (Eric
Benson) writes:

> It appears that you have made a mistake in your testing.

Allow me to be greatly embarrassed.  Eric is correct.  In fact, I had run
both functions interpreted by mistake.  Here are the results for compiled
code:


-------------------------------------------------------------------------------


(defun foo (x)
  (loop for item in x
	collect (atom item)))

Elapsed Real Time = 0.25 seconds
Total Run Time    = 0.25 seconds
User Run Time     = 0.25 seconds
System Run Time   = 0.00 seconds
Process Page Faults    =          0
Dynamic Bytes Consed   =          0
Ephemeral Bytes Consed =    240,000


- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -


(defun foo (x)
  (mapcar #'atom x))

Elapsed Real Time = 0.20 seconds
Total Run Time    = 0.20 seconds
User Run Time     = 0.20 seconds
System Run Time   = 0.00 seconds
Process Page Faults    =          0
Dynamic Bytes Consed   =          0
Ephemeral Bytes Consed =    240,000


-------------------------------------------------------------------------------


This gives me the opportunity (and possibly the obligation) to re-run my
"mirror" tests.  This required creating a new list to reflect.  The one used
here was nested to about eight levels and contained about 60,000 atoms.


-------------------------------------------------------------------------------


My effort:

(defun mirror (x)
  (if (atom x)
      x
      (let ((result nil))
	(dolist (item x)
	  (push (mirror item) result))
	result)))

Elapsed Real Time = 0.61 seconds
Total Run Time    = 0.61 seconds
User Run Time     = 0.61 seconds
System Run Time   = 0.00 seconds
Process Page Faults    =          0
Dynamic Bytes Consed   =          0
Ephemeral Bytes Consed =    612,528


- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -


By Eliot Handelman and Raja Sooriamurthi, modified slightly:

(defun mirror (x)
  (if (atom x)
      x
      (append (mirror (rest x)) (list (mirror (first x))))))

Elapsed Real Time = 1.40 seconds
Total Run Time    = 1.39 seconds
User Run Time     = 1.39 seconds
System Run Time   = 0.00 seconds
Process Page Faults    =          0
Dynamic Bytes Consed   =          0
Ephemeral Bytes Consed =  2,041,384


- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -


By Noritake Yonezawa, using nreverse.

(defun mirror (x) 
  (if (atom x)
      x
      (nreverse (mapcar #'mirror x))))

Elapsed Real Time = 0.75 seconds
Total Run Time    = 0.75 seconds
User Run Time     = 0.75 seconds
System Run Time   = 0.00 seconds
Process Page Faults    =          0
Dynamic Bytes Consed   =          0
Ephemeral Bytes Consed =    612,528


-------------------------------------------------------------------------------


Your mileage may vary.


-Michael Wilk (wilk@cs.cornell.edu)