Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import com.google.common.collect.Sets;
- public class IntersectTest {
- static Random rng = new Random();
- static abstract class RunIt {
- public long count;
- public long nsTime;
- abstract int Run (List<Long> s1, List<Long> s2);
- }
- // As presented in the post
- static class PostMethod1 extends RunIt {
- public int Run(List<Long> a, List<Long> b) {
- SortedSet<Long> aSet = new TreeSet<Long>(a);
- aSet.retainAll(b);
- return new ArrayList<Long>(aSet).size();
- }
- }
- static class PostMethod2 extends RunIt {
- public int Run(List<Long> a, List<Long> b) {
- List<Long> c = new ArrayList<Long>(a); // <-- new requirement.
- c.retainAll(b);
- return c.size();
- }
- }
- static class MyMethod extends RunIt {
- public int Run (List<Long> a, List<Long> b) {
- Set<Long> aSet = new HashSet<Long>(a);
- Set<Long> bSet = new HashSet<Long>(b);
- for(Iterator<Long> it = aSet.iterator(); it.hasNext();) {
- if(!bSet.contains(it.next())) it.remove();
- }
- return new ArrayList<Long>(aSet).size();
- }
- }
- static class Guava extends RunIt {
- public int Run (List<Long> a, List<Long> b) {
- Set<Long> aSet = new HashSet<Long>(a);
- Set<Long> bSet = new HashSet<Long>(b);
- Set<Long> intersection = Sets.intersection(aSet, bSet);
- return new ArrayList<Long>(intersection).size();
- }
- }
- static ArrayList<Long> makeSet (int count, float load) {
- ArrayList<Long> s = new ArrayList<Long>();
- for (int i = 0; i < count; i++) {
- s.add((long)rng.nextInt(Math.max(1, (int)(count * load))));
- }
- return s;
- }
- // really crummy ubench stuff
- public static void main(String[] args) {
- int[][] bounds = {
- {1, 1},
- {10, 10},
- {50, 50},
- {100, 100},
- {500, 500},
- {1000, 1000},
- {5000, 5000},
- {10000, 10000},
- };
- int totalReps = 4;
- int cycleReps = 1000;
- int subReps = 1000;
- float load = 0.8f;
- for (int tc = 0; tc < totalReps; tc++) {
- for (int[] bound : bounds) {
- int set1size = bound[0];
- int set2size = bound[1];
- System.out.println("Running tests for " + set1size + "x" + set2size);
- ArrayList<RunIt> allRuns = new ArrayList<RunIt>(
- Arrays.asList(
- new PostMethod1(),
- new PostMethod2(),
- new MyMethod(),
- new Guava()));
- for (int r = 0; r < cycleReps; r++) {
- ArrayList<RunIt> runs = new ArrayList<RunIt>(allRuns);
- ArrayList<Long> set1 = makeSet(set1size, load);
- ArrayList<Long> set2 = makeSet(set2size, load);
- while (runs.size() > 0) {
- int runIdx = rng.nextInt(runs.size());
- RunIt run = runs.remove(runIdx);
- long start = System.nanoTime();
- int count = 0;
- for (int s = 0; s < subReps; s++) {
- count += run.Run(set1, set2);
- }
- long time = System.nanoTime() - start;
- run.nsTime += time;
- run.count += count;
- }
- }
- for (RunIt run : allRuns) {
- double sec = run.nsTime / (10e6);
- System.out.println(run + " took " + sec + " count=" + run.count);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement