gambas-source-code/benchmark/btree.jvs
Benoît Minisini de200aac4c Add a new 'btree' benchmark, that does a lot of object creation and recursive function calls.
[BENCHMARKS]
* NEW: Add a new 'btree' benchmark, that does a lot of object creation and recursive function calls.
* NEW: Rename the 'string' benchmark as 'string1', to avoid name clashes in Python.
* BUG: Fix the HTML table generation.
2023-10-04 22:10:01 +02:00

52 lines
1.6 KiB
Text
Executable file

#!/usr/bin/java --source 11
public class binarytrees {
public static void main(String[] args) throws Exception {
int n = args.length > 0 ? Integer.parseInt(args[0]) : 0;
int minDepth = 4;
int maxDepth = 16;
int stretchDepth = maxDepth + 1;
int check = (TreeNode.create(stretchDepth)).check();
System.out.println("stretch tree of depth " + (maxDepth + 1) + "\t check: " + check);
TreeNode longLivedTree = TreeNode.create(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.create(depth)).check();
}
System.out.println(iterations + "\t trees of depth " + depth + "\t check: " + check);
}
System.out.println("long lived tree of depth " + maxDepth + "\t check: " + longLivedTree.check());
}
static class TreeNode {
TreeNode left, right;
static TreeNode create(int depth)
{
return ChildTreeNodes(depth);
}
static TreeNode ChildTreeNodes(int depth)
{
TreeNode node = new TreeNode();
if (depth > 0)
{
node.left = ChildTreeNodes(depth - 1);
node.right = ChildTreeNodes(depth - 1);
}
return node;
}
int check() {
return left == null ? 1 : left.check() + right.check() + 1;
}
}
}