Guest User

Untitled

a guest
Aug 5th, 2013
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.26 KB | None | 0 0
  1. import java.util.HashMap;
  2. import java.util.Map;
  3.  
  4.  
  5. interface DecorateMethod {
  6.  
  7.     public void runMe();
  8.  
  9. }
  10.  
  11.  
  12. public class FibonacciFun {
  13.  
  14.     private static Map<Integer, Long> results;
  15.  
  16.     static {
  17.         results = new HashMap<>();
  18.         results.put(0, 0l);
  19.         results.put(1, 1l);
  20.     }
  21.  
  22.     public static long dynamicFib(int n) {
  23.         if (results.containsKey(n)) {
  24.             return results.get(n);
  25.         } else {
  26.             results.put(n, dynamicFib(n - 1) + dynamicFib(n - 2));
  27.             return results.get(n);
  28.         }
  29.     }
  30.  
  31.     public static long dumbFib(int n) {
  32.         if (n == 0)
  33.             return 0;
  34.         else if (n == 1)
  35.             return 1;
  36.         else
  37.             return dumbFib(n - 1) + dumbFib(n - 2);
  38.     }
  39.  
  40.     public static long smartFib(int n) {
  41.         int iteration = 0;
  42.         int a = 0;
  43.         int b = 1;
  44.         while (iteration++ < n) {
  45.             b = a + b;
  46.             a = b - a;
  47.         }
  48.         return a;
  49.     }
  50.  
  51.     public static long arrayFib(int n, boolean printMe) {
  52.         int[] results = new int[n + 1];
  53.         results[0] = 0;
  54.         if (n > 0) {
  55.             results[1] = 1;
  56.             for (int i = 2; i <= n; i++) {
  57.                 results[i] = results[i - 1] + results[i - 2];
  58.             }
  59.         }
  60.         if (printMe) {
  61.             System.out.println(results.length);
  62.         }
  63.         return results[n];
  64.     }
  65.  
  66.     public static long testMethod(DecorateMethod method) {
  67.         long startTime = System.currentTimeMillis();
  68.         for (int i = 0; i < 1000; i++) {
  69.             method.runMe();
  70.         }
  71.         long endTime = System.currentTimeMillis();
  72.         return endTime - startTime;
  73.     }
  74.  
  75.     public static void main(String[] beans) {
  76.         //Test array fib
  77.         System.out.println("Array fib1 " + testMethod(new DecorateMethod() {
  78.             @Override
  79.             public void runMe() {
  80.                 for (int i = 0; i < 20; i++)
  81.                     arrayFib(i, false);
  82.             }
  83.         }));
  84.  
  85.         long start, end;
  86.  
  87.         start = System.currentTimeMillis();
  88.         for (int j = 0; j < 1000; j++) {
  89.             for (int i = 0; i < 20; i++) {
  90.                 long d = (arrayFib(i, false));
  91.             }
  92.         }
  93.         end = System.currentTimeMillis();
  94.         System.out.printf("Array fib %d\n", end - start);
  95.  
  96.  
  97.         start = System.currentTimeMillis();
  98.         for (int j = 0; j < 1000; j++) {
  99.             for (int i = 0; i < 20; i++) {
  100.                 long d = (smartFib(i));
  101.             }
  102.         }
  103.         end = System.currentTimeMillis();
  104.         System.out.printf("Smart fib %d\n", end - start);
  105.  
  106.  
  107.         start = System.currentTimeMillis();
  108.         for (int j = 0; j < 1000; j++) {
  109.             for (int i = 0; i < 20; i++) {
  110.                 long d = (dumbFib(i));
  111.             }
  112.             end = System.currentTimeMillis();
  113.         }
  114.         System.out.printf("Dumb fib %d\n", end - start);
  115.  
  116.         start = System.currentTimeMillis();
  117.         for (int j = 0; j < 1000; j++) {
  118.             for (int i = 0; i < 20; i++) {
  119.                 long d = (dynamicFib(i));
  120.             }
  121.         }
  122.         end = System.currentTimeMillis();
  123.         System.out.printf("Dynamic fib %d\n", end - start);
  124.     }
  125.  
  126. }
Advertisement
Add Comment
Please, Sign In to add comment