#include <iostream>
#include <algorithm>
#include <vector>
#include <unordered_set>
#include <cstdlib>
#include <ctime>
#include <cassert>
#include <set>
typedef unsigned long long ull;
typedef std::pair<int,ull> Hash;
struct getHash {
public:
template <typename T, typename U>
std::size_t operator()(const std::pair<T, U> &x) const
{
return std::hash<T>()(x.first) ^ std::hash<U>()(x.second);
}
};
ull rand_ull() {
ull res = 0;
for (int i = 0; i < 6; ++i) {
(res *= 1000) += (std::rand() % 1000);
}
return res;
}
int main() {
std::srand(std::time(0));
const int n = (int)1e6;
std::vector<Hash> array(n);
for (auto& it : array) {
it = std::make_pair(std::rand() % (int)1e9, rand_ull());
}
std::vector<Hash> queries(n);
for (auto& it : queries) {
it = std::make_pair(std::rand() % (int)1e9, rand_ull());
}
double t = clock();
std::set<Hash> hashtable;
for (auto it : array) {
hashtable.insert(it);
}
int count1 = 0;
for (auto hash : queries) {
count1 += hashtable.find(hash) != hashtable.end();
}
double time1 = (clock() - t) / CLOCKS_PER_SEC;
t = clock();
std::sort(array.begin(), array.end());
int count2 = 0;
for (auto hash : queries) {
count2 += std::binary_search(array.begin(), array.end(), hash);
}
double time2 = (clock() - t) / CLOCKS_PER_SEC;
assert(count1 == count2);
fprintf(stdout, "size & queries = %d\n", n);
fprintf(stdout, " std::set = %0.3fs\n", time1);
fprintf(stdout, "sort+binsearch = %0.3fs\n", time2);
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8YWxnb3JpdGhtPgojaW5jbHVkZSA8dmVjdG9yPgojaW5jbHVkZSA8dW5vcmRlcmVkX3NldD4KI2luY2x1ZGUgPGNzdGRsaWI+CiNpbmNsdWRlIDxjdGltZT4KI2luY2x1ZGUgPGNhc3NlcnQ+CiNpbmNsdWRlIDxzZXQ+Cgp0eXBlZGVmIHVuc2lnbmVkIGxvbmcgbG9uZyB1bGw7CnR5cGVkZWYgc3RkOjpwYWlyPGludCx1bGw+IEhhc2g7CgpzdHJ1Y3QgZ2V0SGFzaCB7CnB1Y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