Advertisement
321igor

Untitled

Mar 7th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.91 KB | None | 0 0
  1. import java.util.Random;
  2.  
  3. public class main {
  4.     static Random rnd = new Random();
  5.  
  6.     public static void main(String args[]) {
  7.         double lastTime;
  8.         System.out.println("Testing trees(10 times: )");
  9.         for (int i = 0; i < 10; i++) {
  10.             lastTime = System.currentTimeMillis();
  11.             minimax(50, 3, 0);
  12.             System.out.println("The tree(function) is done took you: " + (System.currentTimeMillis()-lastTime));
  13.             lastTime = System.currentTimeMillis();
  14.             Tree tree = new Tree(50);
  15.             tree.runTree(0);
  16.             System.out.println("The tree(class) is done took you: " + (System.currentTimeMillis()-lastTime));
  17.             System.out.println();
  18.         }
  19.     }
  20.  
  21.     static double minimax(int nodes, int maxDepth, int depth) {
  22.         if (depth >= maxDepth) {
  23.             return doSomeMath ();
  24.         }
  25.         for (int i = 0; i < nodes; i++) {
  26.             minimax(nodes, maxDepth, depth+1);
  27.         }
  28.         return -1;
  29.     }
  30.  
  31.     static double doSomeMath()
  32.     {
  33.         double random = rnd.nextDouble();
  34.         return Math.pow(Math.E, random) * rnd.nextDouble();
  35.     }
  36. }
  37.  
  38. class Tree
  39. {
  40.     static Random rnd = new Random();
  41.     Tree[] nodes;
  42.     final int maxDepth = 3;
  43.  
  44.     public Tree(int nodes)
  45.     {
  46.         this.nodes = new Tree[nodes];
  47.  
  48.     }
  49.  
  50.     public void initialazeNodes()
  51.     {
  52.         for (int i = 0; i < nodes.length; i++)
  53.             nodes[i] = new Tree(nodes.length);
  54.     }
  55.  
  56.     public void runTree(int depth)
  57.     {
  58.  
  59.         if (depth >= maxDepth)
  60.         {
  61.             doSomeMath();
  62.             return;
  63.         }
  64.         initialazeNodes();
  65.         for (Tree node : nodes)
  66.         {
  67.  
  68.             node.runTree(depth + 1);
  69.         }
  70.     }
  71.  
  72.     public double doSomeMath()
  73.     {
  74.         double random = rnd.nextDouble();
  75.         return Math.pow(Math.E, random) * rnd.nextDouble();
  76.     }
  77.  
  78. }
  79. /*
  80. Memmory:
  81. Class tree: 130MB
  82. Fumction tree: profiler shows 0??! (Probably doesn't count as a data structure)
  83.  
  84. Output(a little less detailed):
  85. Testing trees(10 times: )
  86. The tree(function) is done took you: 95.0
  87. The tree(class) is done took you: 227.0
  88.  
  89. The tree(function) is done took you: 65.0
  90. The tree(class) is done took you: 86.0
  91.  
  92. The tree(function) is done took you: 66.0
  93. The tree(class) is done took you: 85.0
  94.  
  95. The tree(function) is done took you: 67.0
  96. The tree(class) is done took you: 110.0
  97.  
  98. The tree(function) is done took you: 65.0
  99. The tree(class) is done took you: 107.0
  100.  
  101. The tree(function) is done took you: 75.0
  102. The tree(class) is done took you: 84.0
  103.  
  104. The tree(function) is done took you: 66.0
  105. The tree(class) is done took you: 112.0
  106.  
  107. The tree(function) is done took you: 64.0
  108. The tree(class) is done took you: 82.0
  109.  
  110. The tree(function) is done took you: 80.0
  111. The tree(class) is done took you: 96.0
  112.  
  113. The tree(function) is done took you: 65.0
  114. The tree(class) is done took you: 124.0
  115. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement