Advertisement
Guest User

confirmation

a guest
Nov 23rd, 2018
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.05 KB | None | 0 0
  1. private static final int ITERATIONS = 100000000;
  2.  
  3.     public static void main(String[] arguments) {
  4.  
  5.         final int iterations = 20;
  6.         long optionOneTotal = 0;
  7.         long optionTwoTotal = 0;
  8.  
  9.         for (int i = 0; i < iterations; i++) {
  10.  
  11.             optionOneTotal += optionOne();
  12.             optionTwoTotal += optionTwo();
  13.         }
  14.  
  15.         System.out.println("OptionOne time: " + (optionOneTotal / iterations));
  16.         System.out.println("OptionTwo time: " + (optionTwoTotal / iterations));
  17.     }
  18.  
  19.     private static long optionOne() {
  20.  
  21.         long start = System.nanoTime();
  22.         int n = 0;
  23.         for (int i = 0; i < ITERATIONS; i++) {
  24.             n += 2 * i * i;
  25.         }
  26.         System.out.println(n);
  27.         return System.nanoTime() - start;
  28.     }
  29.  
  30.     private static long optionTwo() {
  31.  
  32.         long start = System.nanoTime();
  33.         int n = 0;
  34.         for (int i = 0; i < ITERATIONS; i++) {
  35.             n += 2 * (i * i);
  36.         }
  37.         System.out.println(n);
  38.         return System.nanoTime() - start;
  39.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement