Advertisement
Guest User

Intersection test

a guest
May 13th, 2014
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.86 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. import com.google.common.collect.Sets;
  4.  
  5. public class IntersectTest {
  6.  
  7.     static Random rng = new Random();
  8.  
  9.     static abstract class RunIt {
  10.         public long count;
  11.         public long nsTime;
  12.         abstract int Run (List<Long> s1, List<Long> s2);
  13.     }
  14.  
  15.     // As presented in the post
  16.     static class PostMethod1 extends RunIt {
  17.         public int Run(List<Long> a, List<Long> b) {
  18.             SortedSet<Long> aSet = new TreeSet<Long>(a);
  19.             aSet.retainAll(b);
  20.             return new ArrayList<Long>(aSet).size();
  21.         }
  22.     }
  23.    
  24.     static class PostMethod2 extends RunIt {
  25.         public int Run(List<Long> a, List<Long> b) {
  26.             List<Long> c = new ArrayList<Long>(a); // <-- new requirement.
  27.             c.retainAll(b);
  28.             return c.size();
  29.         }
  30.     }
  31.  
  32.     static class MyMethod extends RunIt {
  33.         public int Run (List<Long> a, List<Long> b) {
  34.             Set<Long> aSet = new HashSet<Long>(a);
  35.             Set<Long> bSet = new HashSet<Long>(b);
  36.  
  37.             for(Iterator<Long> it = aSet.iterator(); it.hasNext();) {
  38.                 if(!bSet.contains(it.next())) it.remove();
  39.             }
  40.             return new ArrayList<Long>(aSet).size();
  41.         }  
  42.     }
  43.    
  44.     static class Guava extends RunIt {
  45.         public int Run (List<Long> a, List<Long> b) {
  46.             Set<Long> aSet = new HashSet<Long>(a);
  47.             Set<Long> bSet = new HashSet<Long>(b);
  48.  
  49.             Set<Long> intersection = Sets.intersection(aSet, bSet);
  50.            
  51.             return new ArrayList<Long>(intersection).size();
  52.         }  
  53.     }
  54.  
  55.     static ArrayList<Long> makeSet (int count, float load) {
  56.         ArrayList<Long> s = new ArrayList<Long>();
  57.         for (int i = 0; i < count; i++) {
  58.             s.add((long)rng.nextInt(Math.max(1, (int)(count * load))));                    
  59.         }
  60.         return s;
  61.     }
  62.  
  63.     // really crummy ubench stuff
  64.     public static void main(String[] args) {
  65.         int[][] bounds = {
  66.                 {1, 1},
  67.                 {10, 10},
  68.                 {50, 50},
  69.                 {100, 100},
  70.                 {500, 500},
  71.                 {1000, 1000},
  72.                 {5000, 5000},
  73.                 {10000, 10000},
  74.         };
  75.         int totalReps = 4;
  76.         int cycleReps = 1000;
  77.         int subReps = 1000;
  78.         float load = 0.8f;
  79.         for (int tc = 0; tc < totalReps; tc++) {
  80.             for (int[] bound : bounds) {
  81.                 int set1size = bound[0];
  82.                 int set2size = bound[1];
  83.                 System.out.println("Running tests for " + set1size + "x" + set2size);              
  84.                 ArrayList<RunIt> allRuns = new ArrayList<RunIt>(
  85.                         Arrays.asList(
  86.                                 new PostMethod1(),
  87.                                 new PostMethod2(),
  88.                                 new MyMethod(),
  89.                                 new Guava()));
  90.                 for (int r = 0; r < cycleReps; r++) {
  91.                     ArrayList<RunIt> runs = new ArrayList<RunIt>(allRuns);
  92.                     ArrayList<Long> set1 = makeSet(set1size, load);
  93.                     ArrayList<Long> set2 = makeSet(set2size, load);
  94.                     while (runs.size() > 0) {
  95.                         int runIdx = rng.nextInt(runs.size());
  96.                         RunIt run = runs.remove(runIdx);
  97.                         long start = System.nanoTime();
  98.                         int count = 0;
  99.                         for (int s = 0; s < subReps; s++) {
  100.                             count += run.Run(set1, set2);
  101.                         }                      
  102.                         long time = System.nanoTime() - start;
  103.                         run.nsTime += time;
  104.                         run.count += count;
  105.                     }
  106.                 }
  107.                 for (RunIt run : allRuns) {
  108.                     double sec = run.nsTime / (10e6);
  109.                     System.out.println(run + " took " + sec + " count=" + run.count);
  110.                 }
  111.             }
  112.         }      
  113.     }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement