The Computer Language
Benchmarks Game

binary-trees Lisp SBCL program

source code

;;   The Computer Language Benchmarks Game
;;; contributed by Manuel Giraud
;;; modified by Nicolas Neuss
;;; modified by Juho Snellman 2005-10-26
;;; modified by Witali Kusnezow 2009-01-20
;;;  * simplified structure of leaf nodes
;;;  * optimize GC usage
;;;  * optimize all functions
;;; modified by Witali Kusnezow 2009-08-20
;;;  * remove GC hacks to satisfy new versions of the sbcl

;;; Node is either (DATA) (for leaf nodes) or an improper list (DATA LEFT . RIGHT)

(defun build-btree (item depth)
  (declare (fixnum item depth))
  (if (zerop depth) (list item)
      (let ((item2 (+ item item))
            (depth-1 (1- depth)))
        (declare (fixnum item2 depth-1))
        (cons item
				(cons (build-btree (the fixnum (1- item2)) depth-1) 
					  (build-btree item2 depth-1))))))

(defun check-node (node)
  (declare (values fixnum))
  (let ((data (car node))
        (kids (cdr node)))
    (declare (fixnum data))
    (if kids
        (- (+ data (check-node (car kids)))
           (check-node (cdr kids)))

(defun loop-depths (max-depth &key (min-depth 4))
  (declare (type fixnum max-depth min-depth))
  (loop for d of-type fixnum from min-depth by 2 upto max-depth do
       (loop with iterations of-type fixnum = (ash 1 (+ max-depth min-depth (- d)))
          for i of-type fixnum from 1 upto iterations
          sum (+ (the fixnum (check-node (build-btree i d)))
                 (the fixnum (check-node (build-btree (- i) d))))
          into result of-type fixnum
            (format t "~D	 trees of depth ~D	 check: ~D~%"
                    (the fixnum (+ iterations iterations )) d result))))

(defun main (&optional (n (parse-integer
                           (or (car (last #+sbcl sb-ext:*posix-argv*
                                          #+cmu  extensions:*command-line-strings*
                                          #+gcl  si::*command-args*))
  (declare (type (integer 0 255) n))
  (format t "stretch tree of depth ~D	 check: ~D~%" (1+ n) (check-node (build-btree 0 (1+ n))))
  (let ((*print-pretty* nil) (long-lived-tree (build-btree 0 n)))
    (loop-depths n)
    (format t "long lived tree of depth ~D	 check: ~D~%" n (check-node long-lived-tree))))

notes, command-line, and program output

32-bit Ubuntu one core
SBCL 1.3.10

Sat, 01 Oct 2016 03:19:55 GMT

cp: 'binarytrees.sbcl' and './binarytrees.sbcl' are the same file
SBCL built with: /usr/local/bin/sbcl --userinit /dev/null --batch --eval '(load "binarytrees.sbcl_compile")'
### START binarytrees.sbcl_compile
(handler-bind ((sb-ext:defconstant-uneql      (lambda (c) (abort c))))      (load (compile-file "binarytrees.sbcl" ))) (save-lisp-and-die "sbcl.core" :purify t)
### END binarytrees.sbcl_compile

; compiling file "/home/dunham/benchmarksgame/bench/binarytrees/binarytrees.sbcl" (written 24 JAN 2013 02:01:14 PM):
; compiling (DEFUN BUILD-BTREE ...)
; compiling (DEFUN CHECK-NODE ...)
; compiling (DEFUN LOOP-DEPTHS ...)
; compiling (DEFUN MAIN ...)

; /home/dunham/benchmarksgame_onecore/binarytrees/tmp/binarytrees.fasl written
; compilation finished in 0:00:00.027
[undoing binding stack and other enclosing state... done]
[saving current Lisp image into sbcl.core:
writing 3120 bytes from the read-only space at 0x1000000
writing 2304 bytes from the static space at 0x1100000
writing 28151808 bytes from the dynamic space at 0x9000000
### START binarytrees.sbcl_run
(main) (quit)
### END binarytrees.sbcl_run

0.62s to complete and log all make actions

/usr/local/bin/sbcl   --noinform --core sbcl.core --userinit /dev/null --load binarytrees.sbcl_run 20

stretch tree of depth 21	 check: -1
2097152	 trees of depth 4	 check: -2097152
524288	 trees of depth 6	 check: -524288
131072	 trees of depth 8	 check: -131072
32768	 trees of depth 10	 check: -32768
8192	 trees of depth 12	 check: -8192
2048	 trees of depth 14	 check: -2048
512	 trees of depth 16	 check: -512
128	 trees of depth 18	 check: -128
32	 trees of depth 20	 check: -32
long lived tree of depth 20	 check: -1