aboutsummaryrefslogtreecommitdiff
path: root/benchmarks/benchmarksgame/binarytrees_6.java
blob: 8fd0b103b5d1be5bae4343e18b1f834d43221d21 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
 * This benchmark has been ported from "The Computer Language Benchmarks Game"
 * suite and slightly modified to fit the benchmarking framework.
 *
 * The original file:
 * https://benchmarksgame-team.pages.debian.net/benchmarksgame/program/binarytrees-java-6.html
 *
 * The Computer Language Benchmarks Game
 * https://salsa.debian.org/benchmarksgame-team/benchmarksgame/
 *
 * contributed by Jarkko Miettinen
 * modified by Chandra Sekar
 * modified by Mike Kruger
 *
 * LICENSE: 3-Clause BSD
 * https://benchmarksgame-team.pages.debian.net/benchmarksgame/license.html
 */

/*
 * Description:     Allocate and deallocate many many binary trees.
 * Main Focus:      TODO
 *
 */

package benchmarks.benchmarksgame;

// CHECKSTYLE.OFF: TypeName
public class binarytrees_6 {
// CHECKSTYLE.ON: TypeName
  private static final int PREDEFINED_DEPTH = 7;
  private static final int minDepth = 4;

  public int bench() {
    int maxDepth = Math.max(minDepth + 2, PREDEFINED_DEPTH);
    int stretchDepth = maxDepth + 1;

    int totalChecks = (TreeNode.create(stretchDepth)).check();

    TreeNode longLivedTree = TreeNode.create(maxDepth);

    for (int depth = minDepth; depth <= maxDepth; depth += 2) {
      int iterations = 1 << (maxDepth - depth + minDepth);
      int check = 0;

      for (int i = 1; i <= iterations; i++) {
        check += (TreeNode.create(depth)).check();
      }
      totalChecks += check;
    }

    totalChecks += longLivedTree.check();

    return totalChecks;
  }

  static class TreeNode {
    TreeNode left;
    TreeNode right;

    static TreeNode create(int depth) {
      return ChildTreeNodes(depth);
    }

    // CHECKSTYLE.OFF: MethodName
    static TreeNode ChildTreeNodes(int depth) {
    // CHECKSTYLE.ON: MethodName
      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;
    }
  }

  static long checksum;

  public void timeBinaryTrees(int iters) {
    long sum = 0;
    for (int j = 0; j < iters; j++) {
      sum += bench();
    }
    checksum = sum;
  }

  public boolean verifyBinaryTrees() {
    int expected = 8798;
    int found = bench();

    if (expected != found) {
      System.out.println("ERROR: Expected " + expected + " but found " + found);
      return false;
    }
    return true;
  }

  public static void main(String[] args) {
    int rc = 0;
    binarytrees_6 obj = new binarytrees_6();
    final long before = System.currentTimeMillis();
    obj.timeBinaryTrees(30);
    final long after = System.currentTimeMillis();

    if (!obj.verifyBinaryTrees()) {
      rc++;
    }
    System.out.println("benchmarks/benchmarksgame/binarytrees_6: " + (after - before));
    System.exit(rc);
  }
}