Taoufik Dachraoui
1/13/2016 10:14:00 PM
On Wednesday, January 13, 2016 at 10:12:14 PM UTC+1, Dimitri Fontaine wrote:
> Taoufik Dachraoui <dachraoui.taoufik@gmail.com> writes:
> > Is it possible to store conses with the car and cdr declared as double-floats
> > in order to avoid boxing; I tried few things without success.
>
> Did you try to use a defstruct for the tree leaves (and nodes maybe
> too)? You could use :type for the structure slots.
>
> --
> dim
I did the following test to compare conses and defstruct:
(defstruct mytree (left) (right))
(defun bar (n &optional ret)
(let ((a (make-mytree)))
(labels ((%bar (a m &optional rest)
(cond
((= m 0)
(setf (mytree-left a) 1.0D0)
(setf (mytree-right a) 1.0D0)
(if (not (null rest))
(%bar (setf (mytree-right (caar rest)) (make-mytree)) (cdar rest) (cdr rest))))
(t
(%bar (setf (mytree-left a) (make-mytree)) (1- m) (cons (cons a (1- m)) rest))))))
(%bar a n))
(if ret a)))
(defun jar (n &optional ret)
(let ((a (cons nil nil)))
(labels ((%bar (a m &optional rest)
(cond
((= m 0)
(setf (car a) 1.0D0)
(setf (cdr a) 1.0D0)
(if (not (null rest))
(%bar (setf (cdr (caar rest)) (cons nil nil)) (cdar rest) (cdr rest))))
(t
(%bar (setf (car a) (cons nil nil)) (1- m) (cons (cons a (1- m)) rest))))))
(%bar a n))
(if ret a)))
(defun bar-sum (a)
(cond
((mytree-p a)
(+ (bar-sum (mytree-left a)) (bar-sum (mytree-right a))))
(t a)))
(defun jar-sum (a)
(cond
((consp a)
(+ (jar-sum (car a)) (jar-sum (cdr a))))
(t a)))
? (time (bar 20)) ; test for defstruct
(BAR 20)
took 4,508 milliseconds (4.508 seconds) to run.
4,361 milliseconds (4.361 seconds, 96.74%) of which was spent in GC.
During that period, and with 2 available CPU cores,
4,452 milliseconds (4.452 seconds) were spent in user mode
39 milliseconds (0.039 seconds) were spent in system mode
50,331,616 bytes of memory allocated.
368 minor page faults, 0 major page faults, 0 swaps.
T
? (time (jar 20)) ; test for conses
(JAR 20)
took 2,255 milliseconds (2.255 seconds) to run.
2,149 milliseconds (2.149 seconds, 95.30%) of which was spent in GC.
During that period, and with 2 available CPU cores,
2,204 milliseconds (2.204 seconds) were spent in user mode
41 milliseconds (0.041 seconds) were spent in system mode
33,554,408 bytes of memory allocated.
3,664 minor page faults, 0 major page faults, 0 swaps.
? (time (bar-sum (bar 20 t))) ; ; test for defstruct
(BAR-SUM (BAR 20 T))
took 5,650 milliseconds (5.650 seconds) to run.
5,309 milliseconds (5.309 seconds, 93.96%) of which was spent in GC.
During that period, and with 2 available CPU cores,
4,673 milliseconds (4.673 seconds) were spent in user mode
110 milliseconds (0.110 seconds) were spent in system mode
83,886,032 bytes of memory allocated.
4,360 minor page faults, 0 major page faults, 0 swaps.
2097152.0D0
? (time (jar-sum (jar 20 t))) ; test for conses
(JAR-SUM (JAR 20 T))
took 2,797 milliseconds (2.797 seconds) to run.
2,564 milliseconds (2.564 seconds, 91.67%) of which was spent in GC.
During that period, and with 2 available CPU cores,
2,307 milliseconds (2.307 seconds) were spent in user mode
72 milliseconds (0.072 seconds) were spent in system mode
67,108,824 bytes of memory allocated.
2,496 minor page faults, 0 major page faults, 0 swaps.
2097152.0D0
?
Kind regards
Taoufik