The Computer Language
Benchmarks Game

binary-trees Lisp SBCL #2 program

source code

;;   The Computer Language Benchmarks Game
;;   http://benchmarksgame.alioth.debian.org/
;;;
;;; 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
;;;
;;; modified by Marko Kocic 2011-02-18
;;;  * add declaim to optimize for speed
;;;

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

(declaim (optimize (speed 3) (debug 0) (space 0) (safety 0)))

(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)))
        data)))

(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
          finally
            (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*))
                               "1"))))
  (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)))
    (purify)
    (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

NOTES:
64-bit Ubuntu quad core
SBCL 1.4.0


Thu, 26 Oct 2017 16:25:13 GMT

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

; compiling file "/home/dunham/benchmarksgame/bench/binarytrees/binarytrees.sbcl-2.sbcl" (written 24 JAN 2013 01:22:33 PM):
; compiling (DECLAIM (OPTIMIZE # ...))
; compiling (DEFUN BUILD-BTREE ...)
; compiling (DEFUN CHECK-NODE ...)
; compiling (DEFUN LOOP-DEPTHS ...)
; file: /home/dunham/benchmarksgame/bench/binarytrees/binarytrees.sbcl-2.sbcl
; in: DEFUN LOOP-DEPTHS
;     (+ MAX-DEPTH MIN-DEPTH (- D))
; ==>
;   (+ (+ MAX-DEPTH MIN-DEPTH) (- D))
; 
; note: forced to do GENERIC-+ (cost 10)
;       unable to do inline fixnum arithmetic (cost 2) because:
;       The first argument is a (INTEGER -9223372036854775808 9223372036854775806), not a FIXNUM.
;       The second argument is a (INTEGER -4611686018427387903
;                                 4611686018427387904), not a FIXNUM.
;       The result is a (VALUES
;                        (INTEGER -13835058055282163711 13835058055282163710)
;                        &OPTIONAL), not a (VALUES FIXNUM &REST T).
;       unable to do inline (signed-byte 64) arithmetic (cost 5) because:
;       The result is a (VALUES
;                        (INTEGER -13835058055282163711 13835058055282163710)
;                        &OPTIONAL), not a (VALUES (SIGNED-BYTE 64) &REST T).
;       etc.

;     (ASH 1 (+ MAX-DEPTH MIN-DEPTH (- D)))
; 
; note: forced to do full call
;       unable to do inline ASH (cost 3) because:
;       The second argument is a (INTEGER -13835058055282163711
;                                 13835058055282163710), not a (UNSIGNED-BYTE 62).
;       unable to do inline ASH (cost 3) because:
;       The second argument is a (INTEGER -13835058055282163711
;                                 13835058055282163710), not a (UNSIGNED-BYTE 62).
;       etc.

;     (+ MAX-DEPTH MIN-DEPTH (- D))
; ==>
;   (+ (+ MAX-DEPTH MIN-DEPTH) (- D))
; 
; note: doing signed word to integer coercion (cost 20), for:
;       the first argument of GENERIC-+
; 
; note: doing signed word to integer coercion (cost 20), for:
;       the second argument of GENERIC-+

; compiling (DEFUN MAIN ...); 
; compilation unit finished
;   printed 4 notes


; /home/dunham/benchmarksgame_quadcore/binarytrees/tmp/binarytrees.sbcl-2.fasl written
; compilation finished in 0:00:00.018
### START binarytrees.sbcl-2.sbcl_run
(main) (quit)
### END binarytrees.sbcl-2.sbcl_run


3.49s to complete and log all make actions

COMMAND LINE:
/opt/src/sbcl-1.4.0/bin/sbcl  --noinform --core sbcl.core --userinit /dev/null --load binarytrees.sbcl-2.sbcl_run 7

UNEXPECTED OUTPUT 

1,4c1,4
< stretch tree of depth 8	 check: -1
< 256	 trees of depth 4	 check: -256
< 64	 trees of depth 6	 check: -64
< long lived tree of depth 7	 check: -1
---
> stretch tree of depth 8	 check: 511
> 128	 trees of depth 4	 check: 3968
> 32	 trees of depth 6	 check: 4064
> long lived tree of depth 7	 check: 255

PROGRAM OUTPUT:
stretch tree of depth 8	 check: -1
256	 trees of depth 4	 check: -256
64	 trees of depth 6	 check: -64
long lived tree of depth 7	 check: -1