The Computer Language
Benchmarks Game

binary-trees Clojure program

source code

;; The Computer Language Benchmarks Game
;; http://benchmarksgame.alioth.debian.org/
;
;; Adapted from the Java -server version
;
;; contributed by Marko Kocic
;; modified by Kenneth Jonsson, restructured to allow usage of 'pmap'
;; modified by Andy Fingerhut to use faster primitive math ops, and
;; deftype instead of defrecord for smaller tree nodes.
;; modified by Rich Hickey for Clojure 1.3
;; *reset*

(ns binarytrees
  (:gen-class))

(set! *warn-on-reflection* true)
(set! *unchecked-math* true)

(definterface ITreeNode
  (^long item [])
  (left [])
  (right []))

;; These TreeNode's take up noticeably less memory than a similar one
;; implemented using defrecord.

(deftype TreeNode [left right ^long item]
  ITreeNode
  (item [this] item)
  (left [this] left)
  (right [this] right))

(defn bottom-up-tree [^long item ^long depth]
  (if (zero? depth)
    (TreeNode. nil nil item)
    (TreeNode.
     (bottom-up-tree (dec (* 2 item))
                     (dec depth))
     (bottom-up-tree (* 2 item)
                     (dec depth))
     item)))

(defn item-check ^long [^TreeNode node]
  (if (nil? (.left node))
    1
    (+ (+ 1
          (item-check (.left node)))
       (+ (item-check (.right node))))))

(defn iterate-trees [^long mx ^long mn ^long d]
  (let [iterations (bit-shift-left 1 (- (+ mx mn) d))]
    (format "%d\t trees of depth %d\t check: %d" iterations d
            (reduce + (map (fn [i]
                             (item-check (bottom-up-tree i d))
                                 )
                           (range 1 (inc iterations)))))))

(def min-depth 4)

(defn main [max-depth]
  (let [stretch-depth (inc max-depth)]
    (let [tree (bottom-up-tree 0 stretch-depth)
          check (item-check tree)]
      (println (format "stretch tree of depth %d\t check: %d" stretch-depth check)))
    (let [long-lived-tree (bottom-up-tree 0 max-depth) ]
      (doseq [trees-nfo (map (fn [d]
                                (iterate-trees max-depth min-depth d))
(range min-depth stretch-depth 2)) ]
        (println trees-nfo))
      (println (format "long lived tree of depth %d\t check: %d" max-depth (item-check long-lived-tree))))))

(defn -main [& args]
  (let [n (if (first args) (Integer/parseInt (first args)) 0)
        max-depth (if (> (+ min-depth 2) n) (+ min-depth 2) n)]
    (main max-depth)
    (shutdown-agents)))
    

notes, command-line, and program output

NOTES:
64-bit Ubuntu quad core
Clojure 1.8.0
java version "1.8.0_45"
Java(TM) SE Runtime Environment (build 1.8.0_45-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.45-b02, mixed mode)


Sun, 26 Mar 2017 03:44:51 GMT

MAKE:
mv binarytrees.clojure binarytrees.clj
/usr/local/src/jdk1.8.0_121/bin/java -Dclojure.compiler.direct-linking=true -Dclojure.compile.path=. -cp .:/usr/local/src/clojure/clojure-1.8.0.jar clojure.lang.Compile binarytrees
Compiling binarytrees to .
1.21s to complete and log all make actions

COMMAND LINE:
/usr/local/src/jdk1.8.0_121/bin/java  -cp .:/usr/local/src/clojure/clojure-1.8.0.jar binarytrees 21

PROGRAM OUTPUT:
stretch tree of depth 22	 check: 8388607
2097152	 trees of depth 4	 check: 65011712
524288	 trees of depth 6	 check: 66584576
131072	 trees of depth 8	 check: 66977792
32768	 trees of depth 10	 check: 67076096
8192	 trees of depth 12	 check: 67100672
2048	 trees of depth 14	 check: 67106816
512	 trees of depth 16	 check: 67108352
128	 trees of depth 18	 check: 67108736
32	 trees of depth 20	 check: 67108832
long lived tree of depth 21	 check: 4194303