Guest User

Untitled

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