跳到正文

科技·工程

对 三种哈希表的测速

ToStars约 13 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年2月15日
篇幅
约 13 分钟

版本

  1. v1当前

三种情景:【动态哈希,插入与访问次数同级】、【动态哈希,访问多于插入】、【静态哈希,先插入所有值后访问,访问远多于查询】

结论:

访问插入同级用 gp 基本上更优。

小规模多查询用 umap,大规模多查询用 cc。

卡空间用 cc。

小规模:

C++
Dynamic Hashing, Equal Inserts and Reads(30/30)
std::unordered_map:
Elapsed time: 1.3e-05 s
gp_hash_table:
Elapsed time: 5.3e-06 s
cc_hash_table:
Elapsed time: 7.8e-06 s

Dynamic Hashing, Read Heavy(10/100)
std::unordered_map:
Elapsed time: 2e-06 s
gp_hash_table:
Elapsed time: 2.9e-06 s
cc_hash_table:
Elapsed time: 2.5e-06 s

Static Hashing(10,300)
std::unordered_map:
Elapsed time: 3.4e-06 s
gp_hash_table:
Elapsed time: 3.7e-06 s
cc_hash_table:
Elapsed time: 3.4e-06 s
 finish

中规模:

C++
Dynamic Hashing, Equal Inserts and Reads(3000/3000)
std::unordered_map:
Elapsed time: 0.0002735 s
gp_hash_table:
Elapsed time: 0.0001745 s
cc_hash_table:
Elapsed time: 0.0002045 s

Dynamic Hashing, Read Heavy(1e3/1e4)
std::unordered_map:
Elapsed time: 0.0001349 s
gp_hash_table:
Elapsed time: 0.0001518 s
cc_hash_table:
Elapsed time: 0.0001461 s

Static Hashing(1e3/3e4)
std::unordered_map:
Elapsed time: 0.0003099 s
gp_hash_table:
Elapsed time: 0.0005181 s
cc_hash_table:
Elapsed time: 0.0003664 s
 finish

大规模

C++
Dynamic Hashing, Equal Inserts and Reads(3e5/3e5)
std::unordered_map:
Elapsed time: 0.0655122 s
gp_hash_table:
Elapsed time: 0.021218 s
cc_hash_table:
Elapsed time: 0.062007 s

Dynamic Hashing, Read Heavy(1e5/1e6)
std::unordered_map:
Elapsed time: 0.0257609 s
gp_hash_table:
Elapsed time: 0.019823 s
cc_hash_table:
Elapsed time: 0.0220123 s

Static Hashing(1e5/3e6)
std::unordered_map:
Elapsed time: 0.0718238 s
gp_hash_table:
Elapsed time: 0.0558085 s
cc_hash_table:
Elapsed time: 0.0443975 s

超大规模:

C++
Dynamic Hashing, Equal Inserts and Reads(6e6/6e6)
std::unordered_map:
Elapsed time: 3.04763 s
gp_hash_table:
Elapsed time: 0.440722 s
cc_hash_table:
Elapsed time: 1.27566 s

Dynamic Hashing, Read Heavy(2e6/2e7)
std::unordered_map:
Elapsed time: 1.07675 s
gp_hash_table:
Elapsed time: 0.663601 s
cc_hash_table:
Elapsed time: 0.890962 s

Static Hashing(2e6,6e7)
std::unordered_map:
Elapsed time: 2.9753 s
gp_hash_table:
Elapsed time: 2.51862 s
cc_hash_table:
Elapsed time: 2.38559 s
 finish

code:

C++
#include <iostream>
#include <unordered_map>
#include <ext/pb_ds/assoc_container.hpp>
#include <chrono>
#include <random>

using namespace __gnu_pbds;

typedef gp_hash_table<int, int> gp_hash_table_t;
typedef cc_hash_table<int, int> cc_hash_table_t;

std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> dis(1, 1000000000);

int acc = 0 ;

template<typename HashTable>
void benchmark_dynamic_equal(HashTable& table, int num_operations) {
    auto start = std::chrono::high_resolution_clock::now();

    for (int i = 0; i < num_operations; ++i) {
        int key = dis(gen);
        table[key] = key;
        auto it = table.find(key);
        if (it != table.end()) {
            [[maybe_unused]]volatile int val = it->second;
            acc ^= val ;
        }
    }

    auto end = std::chrono::high_resolution_clock::now();
    std::chrono::duration<double> elapsed = end - start;
    std::cout << "Elapsed time: " << elapsed.count() << " s\n";
}

template<typename HashTable>
void benchmark_dynamic_read_heavy(HashTable& table, int num_operations, int read_per_write) {
    auto start = std::chrono::high_resolution_clock::now();



    for (int i = 0; i < num_operations; ++i) {
        int key = dis(gen);
        table[key] = key;

        // Perform additional reads based on the read_per_write
        for (int j = 0; j < read_per_write; ++j) {
            int read_key = dis(gen);
            auto it = table.find(read_key);
            if (it != table.end()) {
                [[maybe_unused]]volatile int val = it->second;
                acc ^= val ;
            }
        }
    }

    auto end = std::chrono::high_resolution_clock::now();
    std::chrono::duration<double> elapsed = end - start;
    std::cout << "Elapsed time: " << elapsed.count() << " s\n";
}

template<typename HashTable>
void benchmark_static(HashTable& table, int num_inserts, int num_reads) {
    for (int i = 0; i < num_inserts; ++i) {
        int key = dis(gen);
        table[key] = key;
    }

    auto start = std::chrono::high_resolution_clock::now();

    for (int i = 0; i < num_reads; ++i) {
        int key = dis(gen);
        auto it = table.find(key);
        if (it != table.end()) {
            [[maybe_unused]]volatile int val = it->second;
            acc ^= val ;
        }
    }

    auto end = std::chrono::high_resolution_clock::now();
    std::chrono::duration<double> elapsed = end - start;
    std::cout << "Elapsed time: " << elapsed.count() << " s\n";
}

int main() {

    int num_operations = 6e6 ;
    std::cout << "Dynamic Hashing, Equal Inserts and Reads\n";
    std::unordered_map<int, int> umap;
    std::cout << "std::unordered_map:\n";
    benchmark_dynamic_equal(umap, num_operations);

    gp_hash_table_t gp_table;
    std::cout << "gp_hash_table:\n";
    benchmark_dynamic_equal(gp_table, num_operations);

    cc_hash_table_t cc_table;
    std::cout << "cc_hash_table:\n";
    benchmark_dynamic_equal(cc_table, num_operations);

    std::cout << "\nDynamic Hashing, Read Heavy\n";
    umap.clear();
    gp_table.clear();
    cc_table.clear();

    num_operations = 2e6 ;
    int read_per_write = 10 ; // Number of reads per write in the read-heavy scenario

    std::cout << "std::unordered_map:\n";
    benchmark_dynamic_read_heavy(umap, num_operations, read_per_write);

    std::cout << "gp_hash_table:\n";
    benchmark_dynamic_read_heavy(gp_table, num_operations, read_per_write);

    std::cout << "cc_hash_table:\n";
    benchmark_dynamic_read_heavy(cc_table, num_operations, read_per_write);

    std::cout << "\nStatic Hashing\n";
    umap.clear();
    gp_table.clear();
    cc_table.clear();

    num_operations = 2e6 ;
    read_per_write = 30 ;

    std::cout << "std::unordered_map:\n";
    benchmark_static(umap, num_operations, num_operations * read_per_write);

    std::cout << "gp_hash_table:\n";
    benchmark_static(gp_table, num_operations, num_operations * read_per_write);

    std::cout << "cc_hash_table:\n";
    benchmark_static(cc_table, num_operations, num_operations * read_per_write);



    std::cerr<< " finish\n" ;

    std::cerr<<acc;

    return 0;
}
原文