Skip to content

Instantly share code, notes, and snippets.

@pauljurczak
Created November 20, 2015 19:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pauljurczak/ee310ba49d9b26da9a5f to your computer and use it in GitHub Desktop.
Save pauljurczak/ee310ba49d9b26da9a5f to your computer and use it in GitHub Desktop.
static int setIntersectionBenchmark() throws java.io.IOException {
final int nBags = 1000;
final int maxBagLength = 10;
Scanner scanner = new Scanner(new File("10000ran.dat"));
ArrayList<HashSet<Integer>> bags = new ArrayList<HashSet<Integer>>(nBags);
int similarity = 0;
scanner.useDelimiter(",\\s*");
for (int iBag = 0; iBag < nBags; ++iBag) {
HashSet<Integer> bag = new HashSet<Integer>(maxBagLength);
for (int j = 0; j < maxBagLength; ++j)
bag.add(round(scanner.nextFloat()*maxBagLength));
bags.add(bag);
}
scanner.close();
for (int i = 0; i < nBags; ++i)
for (int j = i+1; j < nBags; ++j) {
HashSet<Integer> s = new HashSet<Integer>(bags.get(i));
s.retainAll(bags.get(j));
similarity += s.size();
}
return similarity;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment