Advertisement
Guest User

SideOfZero.java

a guest
Jun 5th, 2013
682
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.17 KB | None | 0 0
  1. import java.util.Random;
  2.  
  3. import com.google.caliper.Runner;
  4. import com.google.caliper.SimpleBenchmark;
  5.  
  6. public class SideOfZero {
  7.    
  8.     public static class Benchmark1 extends SimpleBenchmark {
  9.         private Random r;
  10.         private int[] nums = new int[2000];
  11.        
  12.         public void setUp() {
  13.             r = new Random();
  14.             for(int i = 0; i < 2000; i++) {
  15.                 nums[i] = r.nextInt();
  16.             }
  17.         }
  18.        
  19.         public int timeXOR(int reps) {
  20.             int total = 0;
  21.             for(int i = 0; i < reps; i++) {
  22.                 for(int j = 0; j < 1000; j++) {
  23.                     if(nums[2*j] == 0 || nums[2*j+1] == 0) {
  24.                         // nothing
  25.                     } else if((nums[2*j] ^ nums[2*j+1]) < 0) {
  26.                         total++;
  27.                     } else {
  28.                         total--;
  29.                     }
  30.                 }
  31.             }
  32.             return total;
  33.         }
  34.        
  35.         public int timeIfs(int reps) {
  36.             int total = 0;
  37.             for(int i = 0; i < reps; i++) {
  38.                 for(int j = 0; j < 1000; j++) {
  39.                     if(nums[2*j] == 0 || nums[2*j+1] == 0) {
  40.                         // nothing
  41.                     } else if((nums[2*j] > 0 && nums[2*j+1] > 0) ||
  42.                             (nums[2*j] < 0 && nums[2*j+1] < 0)) {
  43.                         total++;
  44.                     } else {
  45.                         total--;
  46.                     }
  47.                 }
  48.             }
  49.             return total;
  50.         }
  51.     }
  52.    
  53.     public static void main(String... args) {
  54.         Runner.main(Benchmark1.class, args);
  55.     }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement