The Computer Language
24.06 Benchmarks Game

binary-trees Dart jit #6 program

source code

/* The Computer Language Benchmarks Game
   https://salsa.debian.org/benchmarksgame-team/benchmarksgame/

   transliterated from Andrey Filatkin's Node #6 program by Isaac Gouy
*/

void main(List<String> args) {
  final maxDepth = (args.length > 0) ? int.parse(args[0]) : 6;

  final stretchDepth = maxDepth + 1;
  final check = itemCheck(bottomUpTree(stretchDepth));
  print("stretch tree of depth $stretchDepth\t check: $check");

  final longLivedTree = bottomUpTree(maxDepth);

  for (var depth = 4; depth <= maxDepth; depth += 2) {
    final iterations = 1 << maxDepth - depth + 4;
    work(iterations, depth);
  }

  print(
      "long lived tree of depth $maxDepth\t check: ${itemCheck(longLivedTree)}");
}

void work(int iterations, int depth) {
  var check = 0;
  for (int i = 0; i < iterations; i++) {
    check += itemCheck(bottomUpTree(depth));
  }
  print("${iterations}\t trees of depth $depth\t check: $check");
}

class TreeNode {
  TreeNode? left, right;
  TreeNode(this.left, this.right);
}

int itemCheck(TreeNode? node) {
  if (node == null || node.left == null) {
    return 1;
  } else {
    return 1 + itemCheck(node.left) + itemCheck(node.right);
  }
}

TreeNode bottomUpTree(int depth) {
  return depth > 0
      ? TreeNode(bottomUpTree(depth - 1), bottomUpTree(depth - 1))
      : TreeNode(null, null);
}
    

notes, command-line, and program output

NOTES:
64-bit Ubuntu quad core
Dart SDK version: 3.4.1 (stable)
Tue May 21 15:46:25 2024



 Tue, 28 May 2024 21:42:50 GMT

MAKE:
/opt/src/dart-sdk/bin/dart analyze 
Analyzing tmp...
No issues found!

2.89s to complete and log all make actions

COMMAND LINE:
 /opt/src/dart-sdk/bin/dart run  binarytrees.dartjit-6.dartjit 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