You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

__gnu_cxx::hash以指针为键性能极差,unordered_map为何快很多?

__gnu_cxx::hash_map 与 std::unordered_map 插入性能对比

我做了100万次插入操作的性能测试,结果差异非常显著:

  • 基于__gnu_cxx::hash的hash_map耗时约224秒
  • std::unordered_map仅需2秒

测试核心代码如下:

#include <ext/hash_map>

typedef __gnu_cxx::hash_map<char*, std::string> hashMap;
typedef std::unordered_map<char*, std::string> stdMap;

hashMap hM;
int N = 1000000;
for (int i = 0; i < N; i++) {
    std::string ss = std::to_string(i+1);
    char *id = new char;
    hM.insert(make_pair(id, ss));
}

另外发现一个关键细节:如果将键的类型从char*改为char或int,hash_map的耗时也能降至2秒以内。

内容的提问来源于stack exchange,提问作者Aragon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 02:55:24