Permalink
Browse files

Replace Java binarytrees program with the Feb 21 2013 fastest on the …

…4-core 64-bit benchmark machine
  • Loading branch information...
jafingerhut committed Feb 21, 2013
1 parent 0633d5b commit f320ccd862eecc115827b80d3d1c32cb5061d040
@@ -9,6 +9,7 @@
+
<meta content="text/html; charset=ISO-8859-1" http-equiv="content-type"><title>Clojure whole program benchmarks</title>
<meta content="Andy Fingerhut" name="author"></head><body>[<a href="Clojure-benchmarks.html">benchmarks main page</a>] [whole program benchmarks] [<a href="Clojure-expression-benchmarks.html">expression benchmarks</a>] [<a href="Clojure-version-history.html">Clojure version history</a>] [<a href="Hardware-and-software-used-for-Clojure-benchmarks.html">hardware and software used</a>]
<h1><a class="mozTocH1" name="mozTocId411050"></a>Clojure whole program benchmarks</h1>For each program, the tables below include:<br>
@@ -98,7 +99,7 @@ <h1><a class="mozTocH1" name="mozTocId411050"></a>Clojure whole program benchmar
</td>
<td>Memory allocation and deallocation.&nbsp; Inherently sequential (true?) except if GC can run in parallel.<br>
</td>
- <td>[<a href="binarytrees.java-2.java">Java</a>]<br>
+ <td>[<a href="binarytrees.java-1.java">Java</a>]<br>
[<a href="binarytrees.clojure-rh.clojure">Clojure</a>]<br>
[Results]<br>
</td>
@@ -0,0 +1,137 @@
+/* The Computer Language Benchmarks Game
+ http://shootout.alioth.debian.org/
+
+ contributed by Leonhard Holz
+ based on contribution by Jarkko Miettinen
+*/
+
+public class binarytrees {
+
+ private final static int minDepth = 4;
+ private final static int threadCount = Runtime.getRuntime().availableProcessors() > 1 ? 2 : 1;
+ private final static TreeGenerator[] threads = new TreeGenerator[threadCount + 1];
+
+ public static void main(String[] args)
+ {
+ int n = 0;
+ if (args.length > 0) n = Integer.parseInt(args[0]);
+ int maxDepth = (minDepth + 2 > n) ? minDepth + 2 : n;
+
+ for (int i = 0; i < threadCount + 1; i++) {
+ threads[i] = new TreeGenerator();
+ threads[i].start();
+ }
+
+ TreeGenerator lastThread = threads[threadCount];
+ lastThread.depth = maxDepth + 1;
+ lastThread.run = true;
+ try {
+ synchronized(lastThread) {
+ lastThread.notify();
+ lastThread.wait();
+ }
+ } catch (InterruptedException e) {}
+
+ System.out.println("stretch tree of depth " + lastThread.depth + "\t check: " + lastThread.result);
+
+ lastThread.depth = maxDepth;
+ lastThread.run = true;
+ try {
+ synchronized(lastThread) {
+ lastThread.notify();
+ lastThread.wait();
+ }
+ } catch (InterruptedException e) {}
+
+ for (int depth = minDepth; depth <= maxDepth; depth+=2 ) {
+
+ int check = 0;
+ int iterations = 1 << (maxDepth - depth + minDepth);
+ int length = iterations / threadCount;
+
+ for (int i = 0; i < threadCount; i++) synchronized(threads[i]) {
+ threads[i].depth = depth;
+ threads[i].start = i * length;
+ threads[i].end = (i + 1) * length;
+ threads[i].run = true;
+ threads[i].notify();
+ }
+ for (int i = 0; i < threadCount; i++) try {
+ synchronized(threads[i]) {
+ if (threads[i].run) threads[i].wait();
+ }
+ check += threads[i].result;
+ } catch (InterruptedException e) {}
+
+ System.out.println((iterations * 2) + "\t trees of depth " + depth + "\t check: " + check);
+ }
+
+ System.out.println("long lived tree of depth " + maxDepth + "\t check: "+ lastThread.result);
+
+ for (int i = 0; i < threadCount + 1; i++) {
+ threads[i].terminate = true;
+ synchronized(threads[i]) {
+ threads[i].notify();
+ }
+ }
+ }
+
+ private static class TreeGenerator extends Thread
+ {
+ private boolean run = false;
+ private boolean terminate = false;
+
+ private int start = 0;
+ private int end = 0;
+ private int result = 0;
+ private int depth;
+
+ private static TreeNode bottomUpTree(int item, int depth)
+ {
+ TreeNode node = new TreeNode();
+ node.item = item;
+ if (depth > 0) {
+ node.left = bottomUpTree(2 * item - 1, depth - 1);
+ node.right = bottomUpTree(2 * item, depth - 1);
+ } else {
+ node.left = null;
+ }
+ return node;
+ }
+
+ private static int checkItems(TreeNode node)
+ {
+ if (node.left == null) {
+ return node.item;
+ } else {
+ return node.item + checkItems(node.left) - checkItems(node.right);
+ }
+ }
+
+
+ public synchronized void run()
+ {
+ while (!terminate) {
+ if (run) {
+ result = 0;
+ if (start == end) {
+ result += checkItems(bottomUpTree(start, depth));
+ } else for (int i = start; i < end; i++) {
+ result += checkItems(bottomUpTree(i, depth)) + checkItems(bottomUpTree(-i, depth));
+ }
+ run = false;
+ notify();
+ }
+ try {
+ wait();
+ } catch (InterruptedException e) {}
+ }
+ }
+ }
+
+ private static class TreeNode
+ {
+ private int item;
+ private TreeNode left, right;
+ }
+}
@@ -1,71 +0,0 @@
-/* The Great Computer Language Shootout
- http://shootout.alioth.debian.org/
-
- contributed by Jarkko Miettinen
-*/
-
-public class binarytrees {
-
- private final static int minDepth = 4;
-
- public static void main(String[] args){
- int n = 0;
- if (args.length > 0) n = Integer.parseInt(args[0]);
-
- int maxDepth = (minDepth + 2 > n) ? minDepth + 2 : n;
- int stretchDepth = maxDepth + 1;
-
- int check = (TreeNode.bottomUpTree(0,stretchDepth)).itemCheck();
- System.out.println("stretch tree of depth "+stretchDepth+"\t check: " + check);
-
- TreeNode longLivedTree = TreeNode.bottomUpTree(0,maxDepth);
-
- for (int depth=minDepth; depth<=maxDepth; depth+=2){
- int iterations = 1 << (maxDepth - depth + minDepth);
- check = 0;
-
- for (int i=1; i<=iterations; i++){
- check += (TreeNode.bottomUpTree(i,depth)).itemCheck();
- check += (TreeNode.bottomUpTree(-i,depth)).itemCheck();
- }
- System.out.println((iterations*2) + "\t trees of depth " + depth + "\t check: " + check);
- }
- System.out.println("long lived tree of depth " + maxDepth + "\t check: "+ longLivedTree.itemCheck());
- }
-
-
- private static class TreeNode
- {
- private TreeNode left, right;
- private int item;
-
- TreeNode(int item){
- this.item = item;
- }
-
- private static TreeNode bottomUpTree(int item, int depth){
- if (depth>0){
- return new TreeNode(
- bottomUpTree(2*item-1, depth-1)
- , bottomUpTree(2*item, depth-1)
- , item
- );
- }
- else {
- return new TreeNode(item);
- }
- }
-
- TreeNode(TreeNode left, TreeNode right, int item){
- this.left = left;
- this.right = right;
- this.item = item;
- }
-
- private int itemCheck(){
- // if necessary deallocate here
- if (left==null) return item;
- else return item + left.itemCheck() - right.itemCheck();
- }
- }
-}

0 comments on commit f320ccd

Please sign in to comment.