Hi,
blindly copying code is usually not a good way to learn a new
language....
I don't know, whether this is more idiomatic Clojure code, but
it works...
(defn build-tree
[item depth]
(when (< 0 depth)
(let [i (* 2 item)
d (dec depth)]
[item (build-tree (dec i) d) (build-tree i d)])))
(defn check-node
[z]
(if z
(+ (z 0) (check-node (z 1)) (- (check-node (z 2))))
0))
(defn iterate-trees
[mx mn d]
(let [iterations (bit-shift-left 1 (+ mx mn (- d)))]
(println (* 2 iterations) "\ttrees of depth" d "\tcheck:"
(reduce + (map (fn [i]
(+ (check-node (build-tree i d))
(check-node (build-tree (- i) d))))
(range 1 (inc iterations)))))))
(defn main
[max-depth]
(let [min-depth 4
str-depth (inc max-depth)]
(let [tree (build-tree 0 str-depth)
x (check-node tree)]
(println "stretch tree of depth" str-depth "\tcheck:" x))
(let [long-lived-tree (build-tree 0 max-depth)]
(doseq d (range min-depth str-depth 2)
(iterate-trees max-depth min-depth d))
(println "long lived tree of depth" max-depth "\tcheck:"
(check-node long-lived-tree)))))
> Armed Bear
> Interpreted 232.54
> Compiled 35.3
> CMUCL
> Interpreted 600.15
> Compiled 6.13
> Clojure 57.131432
>
> These are not formal benchmark tests; each test is of one run, not
> averaged over several, and is performed on my development machine
> which has many other processes running.
user=> (time (main 16))
stretch tree of depth 17 check: -1
131072 trees of depth 4 check: -131072
32768 trees of depth 6 check: -32768
8192 trees of depth 8 check: -8192
2048 trees of depth 10 check: -2048
512 trees of depth 12 check: -512
128 trees of depth 14 check: -128
32 trees of depth 16 check: -32
long lived tree of depth 16 check: -1
"Elapsed time: 24222.279088 msecs"
nil
That is 24.2 seconds on my crappy 1.7 GHz Office machine.
Sincerely
Meikel
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---