Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Random;
- public class main {
- static Random rnd = new Random();
- public static void main(String args[]) {
- double lastTime;
- System.out.println("Testing trees(10 times: )");
- for (int i = 0; i < 10; i++) {
- lastTime = System.currentTimeMillis();
- minimax(50, 3, 0);
- System.out.println("The tree(function) is done took you: " + (System.currentTimeMillis()-lastTime));
- lastTime = System.currentTimeMillis();
- Tree tree = new Tree(50);
- tree.runTree(0);
- System.out.println("The tree(class) is done took you: " + (System.currentTimeMillis()-lastTime));
- System.out.println();
- }
- }
- static double minimax(int nodes, int maxDepth, int depth) {
- if (depth >= maxDepth) {
- return doSomeMath ();
- }
- for (int i = 0; i < nodes; i++) {
- minimax(nodes, maxDepth, depth+1);
- }
- return -1;
- }
- static double doSomeMath()
- {
- double random = rnd.nextDouble();
- return Math.pow(Math.E, random) * rnd.nextDouble();
- }
- }
- class Tree
- {
- static Random rnd = new Random();
- Tree[] nodes;
- final int maxDepth = 3;
- public Tree(int nodes)
- {
- this.nodes = new Tree[nodes];
- }
- public void initialazeNodes()
- {
- for (int i = 0; i < nodes.length; i++)
- nodes[i] = new Tree(nodes.length);
- }
- public void runTree(int depth)
- {
- if (depth >= maxDepth)
- {
- doSomeMath();
- return;
- }
- initialazeNodes();
- for (Tree node : nodes)
- {
- node.runTree(depth + 1);
- }
- }
- public double doSomeMath()
- {
- double random = rnd.nextDouble();
- return Math.pow(Math.E, random) * rnd.nextDouble();
- }
- }
- /*
- Memmory:
- Class tree: 130MB
- Fumction tree: profiler shows 0??! (Probably doesn't count as a data structure)
- Output(a little less detailed):
- Testing trees(10 times: )
- The tree(function) is done took you: 95.0
- The tree(class) is done took you: 227.0
- The tree(function) is done took you: 65.0
- The tree(class) is done took you: 86.0
- The tree(function) is done took you: 66.0
- The tree(class) is done took you: 85.0
- The tree(function) is done took you: 67.0
- The tree(class) is done took you: 110.0
- The tree(function) is done took you: 65.0
- The tree(class) is done took you: 107.0
- The tree(function) is done took you: 75.0
- The tree(class) is done took you: 84.0
- The tree(function) is done took you: 66.0
- The tree(class) is done took you: 112.0
- The tree(function) is done took you: 64.0
- The tree(class) is done took you: 82.0
- The tree(function) is done took you: 80.0
- The tree(class) is done took you: 96.0
- The tree(function) is done took you: 65.0
- The tree(class) is done took you: 124.0
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement