JerboaBurrow / Hop

Lightweight, cross-platform, 2D game engine | ECS; Lua Console; Physics; Tile based
MIT License
0 stars 1 forks source link

assess sparsehash for high performance hashing #145

Closed Jerboa-app closed 9 months ago

Jerboa-app commented 9 months ago

https://github.com/sparsehash/sparsehash

https://stackoverflow.com/questions/3300525/super-high-performance-c-c-hash-map-table-dictionary

Jerboa-app commented 9 months ago

https://stackoverflow.com/questions/74269252/why-c-implementation-using-stdunordered-map-is-much-slower-than-equivalent-p

Jerboa-app commented 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
Jerboa-app commented 9 months ago

146 we will use it for the gain