Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Alioth binary-trees
- Loading branch information
1 parent
eef82b2
commit 868b392
Showing
3 changed files
with
103 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#!/bin/sh | ||
CLASSPATH=src/main/clojure:`cat script/maven-classpath` | ||
|
||
java -server -Xmx2G -Xms2G -Xmn256m -XX:-PrintGCDetails -XX:+DisableExplicitGC -cp $CLASSPATH clojure.main | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
; Copyright (c) Rich Hickey and contributors. | ||
; All rights reserved. | ||
; The use and distribution terms for this software are covered by the | ||
; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) | ||
; which can be found in the file epl-v10.html at the root of this distribution. | ||
; By using this software in any fashion, you are agreeing to be bound by | ||
; the terms of this license. | ||
; You must not remove this notice, or any other, from this software. | ||
|
||
; Alioth benchmarks: http://shootout.alioth.debian.org/u64q/benchmark.php?test=binarytrees&lang=all | ||
; Inspired by http://shootout.alioth.debian.org/u64q/program.php?test=binarytrees&lang=java&id=1 | ||
; and http://shootout.alioth.debian.org/u64q/program.php?test=binarytrees&lang=clojure&id=5 ; | ||
(ns alioth.binary-trees) | ||
|
||
(set! *warn-on-reflection* true) | ||
(set! *unchecked-math* true) | ||
|
||
(definterface ITreeNode | ||
(^long item []) | ||
(left []) | ||
(right [])) | ||
|
||
(deftype TreeNode [left right ^long item] | ||
ITreeNode | ||
(^long item [this] item) | ||
(left [this] left) | ||
(right [this] right)) | ||
|
||
(defn make-tree [^long item ^long depth] | ||
(if (zero? depth) | ||
(TreeNode. nil nil item) | ||
(TreeNode. | ||
(make-tree (dec (* 2 item)) | ||
(dec depth)) | ||
(make-tree (* 2 item) | ||
(dec depth)) | ||
item))) | ||
|
||
(defn item-check ^long [^TreeNode node] | ||
(if (nil? (.left node)) | ||
(.item node) | ||
(+ (+ (.item node) | ||
(item-check (.left node))) | ||
(- (item-check (.right node)))))) | ||
|
||
(defn iterate-trees [mx mn d] | ||
(let [iterations (bit-shift-left 1 (long (+ mx mn (- d))))] | ||
(format "%d\t trees of depth %d\t check: %d" | ||
(* 2 iterations) | ||
d | ||
(loop [result 0 | ||
i 1] | ||
(if (= i (inc iterations)) | ||
result | ||
(recur (+ result | ||
(item-check (make-tree i d)) | ||
(item-check (make-tree (- i) d))) | ||
(inc i))))))) | ||
|
||
(def min-depth 4) | ||
|
||
(defn main [max-depth] | ||
(let [stretch-depth (inc max-depth)] | ||
(let [tree (make-tree 0 stretch-depth) | ||
check (item-check tree)] | ||
(println (format "stretch tree of depth %d\t check: %d" stretch-depth check))) | ||
(let [agents (repeatedly (.availableProcessors (Runtime/getRuntime)) #(agent [])) | ||
long-lived-tree (make-tree 0 max-depth)] | ||
(loop [depth min-depth | ||
[a & more] (cycle agents)] | ||
(if (>= depth stretch-depth) | ||
(do | ||
(doseq [a agents] (await a)) | ||
(doseq [trees-nfo (if (= 1 (count agents)) | ||
@(first agents) | ||
(apply interleave (map deref agents)))] | ||
(println trees-nfo))) | ||
(do | ||
(send a (fn [coll] (conj coll (iterate-trees max-depth min-depth depth)))) | ||
(recur (+ 2 depth) more)))) | ||
(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))) |