Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

AOT compile, script/run, iterative print of binary_trees output

  • Loading branch information...
commit 15b8d7cf33684649bc8efba41ba18b30f970030b 1 parent 868b392
@stuarthalloway stuarthalloway authored
View
16 pom.xml
@@ -24,4 +24,20 @@
</dependency>
</dependencies>
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>com.theoryinpractise</groupId>
+ <artifactId>clojure-maven-plugin</artifactId>
+ <version>1.3.7</version>
+ <extensions>true</extensions>
+ <configuration>
+ <warnOnReflection>${clojure.warnOnReflection}</warnOnReflection>
+ <temporaryOutputDirectory>false</temporaryOutputDirectory>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+
+
</project>
View
2  script/repl
@@ -1,6 +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
+java -server -XX:+AggressiveOpts -cp $CLASSPATH clojure.main
View
4 script/run
@@ -0,0 +1,4 @@
+#!/bin/sh
+CLASSPATH='target/*':src/main/clojure:`cat script/maven-classpath`
+
+java -server -XX:+AggressiveOpts -cp $CLASSPATH $@
View
41 src/main/clojure/alioth/binary_trees.clj
@@ -10,30 +10,22 @@
; 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)
+(ns alioth.binary-trees
+ (:gen-class))
(set! *warn-on-reflection* true)
(set! *unchecked-math* true)
-(definterface ITreeNode
- (^long item [])
- (left [])
- (right []))
+(def min-depth 4)
-(deftype TreeNode [left right ^long item]
- ITreeNode
- (^long item [this] item)
- (left [this] left)
- (right [this] right))
+(deftype TreeNode [left right ^int item])
(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))
+ (make-tree (dec (* 2 item)) (dec depth))
+ (make-tree (* 2 item) (dec depth))
item)))
(defn item-check ^long [^TreeNode node]
@@ -57,8 +49,6 @@
(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)
@@ -67,17 +57,14 @@
(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))))
+ [a & more] (cycle agents)
+ results []]
+ (if (> depth stretch-depth)
+ (doseq [r results] (println @r))
+ (let [result (promise)]
+ (send a (fn [_]
+ (deliver result (iterate-trees max-depth min-depth depth))))
+ (recur (+ 2 depth) more (conj results result)))))
(println (format "long lived tree of depth %d\t check: %d" max-depth (item-check long-lived-tree))))))
(defn -main [& args]
Please sign in to comment.
Something went wrong with that request. Please try again.