The Computer Language
24.11 Benchmarks Game

binary-trees Java GraalVM #3 program

source code

/* The Computer Language Benchmarks Game
 * https://salsa.debian.org/benchmarksgame-team/benchmarksgame/
 *
 * contributed by Jarkko Miettinen
 * modified by Daryl Griffith
 * *reset*
*/

public class binarytrees {

    public static void main(String[] args) {
        int maxDepth;
        
        {
            int n = 0;
            
            if (args.length > 0) {
                n = Integer.parseInt(args[0]);
            }
            maxDepth = (6 > n) ? 6 : n;
        }
        {
            int stretchDepth = maxDepth + 1;

            System.out.println("stretch tree of depth " + stretchDepth + 
		"\t check: " + checkTree(createTree(stretchDepth)));
        }
        trees(maxDepth);
    }
    
    public static void trees(int maxDepth) {
        TreeNode longLastingNode = createTree(maxDepth);
        int depth = 4;

        do {
            int iterations = 16 << (maxDepth - depth);

            loops(iterations, depth);
            depth += 2;
        } while (depth <= maxDepth);
        System.out.println("long lived tree of depth " + maxDepth
		 + "\t check: " + checkTree(longLastingNode));
    }
    
    public static void loops(int iterations, int depth) {
        int check = 0;
        int item = 0;

        do {
            check += checkTree(createTree(depth));
            item++;
        } while (item < iterations);
        System.out.println(iterations + "\t trees of depth " +
		depth + "\t check: " + check);
    }
    
    public static TreeNode createTree(int depth) {
        TreeNode node = new TreeNode();

        if (depth > 0) {
            depth--;
            node.left = createTree(depth);
            node.right = createTree(depth);
        }
        return node;
    }
    
    public static int checkTree(TreeNode node) {
        if (node.left == null) {
            return 1;
        }
        return checkTree(node.left) + checkTree(node.right) + 1;
    }

    public static class TreeNode {

        private int item;
        private TreeNode left, right;
    }
}
    

notes, command-line, and program output

NOTES:
64-bit Ubuntu quad core
openjdk 23 2024-09-17
Java HotSpot(TM) 64-Bit Server VM
OpenJDK GraalVM CE
(build 23+37-jvmci-b01, 
mixed mode, sharing)


 Fri, 20 Sep 2024 19:26:00 GMT

MAKE:
mv binarytrees.graalvm-3.graalvm binarytrees.java
/opt/src/graalvm-jdk-23/bin/javac -d . -cp .  binarytrees.java

2.00s to complete and log all make actions

COMMAND LINE:
 /opt/src/graalvm-jdk-23/bin/java  -cp . 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