Skip to content

Instantly share code, notes, and snippets.

@pauljurczak
Created November 21, 2015 02:37
Show Gist options
  • Save pauljurczak/18753bf3dd9b25b3e5f0 to your computer and use it in GitHub Desktop.
Save pauljurczak/18753bf3dd9b25b3e5f0 to your computer and use it in GitHub Desktop.
int vectorIntersectionBenchmarkLoopOpt1()
{
const int nBags = 1000;
const int maxBagLength = 10;
vector<vector<int>> bags;
ifstream input("10000ran.dat"); // http://www.agner.org/random/10000ran.zip
int similarity = 0;
bags.reserve(nBags);
for (int iBag = 0; iBag < nBags; ++iBag) {
vector<int> bag;
for (int j = 0; j < maxBagLength; ++j) {
string number;
getline(input, number, ',');
bag.push_back(round(atof(number.c_str())*maxBagLength));
}
sort (bag.begin(), bag.end());
bag.erase (unique(bag.begin(), bag.end()), bag.end());
bags.emplace_back(bag);
}
vector<int> intersec;
for (int i = 0; i < bags.size(); ++i)
for (int j = i+1; j < bags.size(); ++j) {
intersec.clear();
set_intersection(bags[i].begin(), bags[i].end(), bags[j].begin(), bags[j].end(), inserter(intersec, intersec.begin()));
similarity += intersec.size();
}
return similarity;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment