Closed Jerboa-app closed 9 months ago
It is a little faster
#include <chrono>
#include <iostream>
#include <map>
#include <unordered_map>
#include <random>
#include <sparsehash/dense_hash_map>
int main()
{
std::unordered_map<uint64_t, uint64_t> map;
const unsigned n = 10000000;
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<uint64_t> U(0, 1024);
for (unsigned i = 0; i < n; i++)
{
map[U(gen)] = U(gen);
}
auto t = std::chrono::high_resolution_clock::now();
uint64_t sum = 0;
for (unsigned i = 0; i < n; i++)
{
sum += map[i];
}
double time = std::chrono::duration<double>(std::chrono::high_resolution_clock::now() - t).count();
std::cout << "std::unordered_map " << sum << ", " << time << " s | " << time / double(n) << " access/s, n = " << n << "\n";
google::dense_hash_map<uint64_t, uint64_t> dmap;
dmap.set_empty_key(-1);
for (unsigned i = 0; i < n; i++)
{
dmap[U(gen)] = U(gen);
}
t = std::chrono::high_resolution_clock::now();
sum = 0;
for (unsigned i = 0; i < n; i++)
{
sum += dmap[i];
}
time = std::chrono::duration<double>(std::chrono::high_resolution_clock::now() - t).count();
std::cout << "sparsehash (google) " << sum << ", " << time << " s | " << time / double(n) << " access/s, n = " << n << "\n";
}
std::unordered_map 546835, 0.793618 s | 7.93618e-08 access/s, n = 10000000
sparsehash (google) 520390, 0.54568 s | 5.4568e-08 access/s, n = 10000000
https://github.com/sparsehash/sparsehash
https://stackoverflow.com/questions/3300525/super-high-performance-c-c-hash-map-table-dictionary