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

为何tbb::concurrent_hash_map读写慢于std::unordered_map?求高效替代方案

哈希表性能疑问与测试分析

核心问题

为什么使用tbb::parallel_for后,tbb::concurrent_hash_map的读写性能仍低于std::unordered_map?尤其是读取耗时表现差距明显。

耗时统计结果

std::unordered_map time:267ms
-------------------find time:55ms

tbb::concurrent_hash_map tbb parrallel time:295ms
-------------------find time:235ms

测试代码

std::unordered_map 测试代码

const int kNumElements = 10'000'000;
// TODO: 1. test unordered_map
std::unordered_map<int, int> map_int_unordered(kNumElements);
auto start_time = std::chrono::steady_clock::now();
for (int i = 0; i < kNumElements; i++)
{
    map_int_unordered[i] = i;
}
auto end_time = std::chrono::steady_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end_time - start_time);
std::cout << DEBUG << "1. std::unordered_map time:" << duration.count() << "ms" << std::endl;
// print_map(map_int_unordered);

start_time = std::chrono::steady_clock::now();
for (int i = 0; i < kNumElements; i++)
{
    map_int_unordered.find(i);
}
end_time = std::chrono::steady_clock::now();
duration = std::chrono::duration_cast<std::chrono::milliseconds>(end_time - start_time);
std::cout << GREEN << " -------------------find time:" << duration.count() << "ms" << std::endl;

tbb::concurrent_hash_map 测试代码

tbb::concurrent_hash_map<int, int> map_tbb2(kNumElements);
start_time = std::chrono::steady_clock::now();
tbb::parallel_for(0, kNumElements, [&](int i)
                  { map_tbb2.insert(std::make_pair(i, i)); });
end_time = std::chrono::steady_clock::now();
duration = std::chrono::duration_cast<std::chrono::milliseconds>(end_time - start_time);
std::cout << DEBUG << "3. tbb::concurrent_hash_map tbb parrallel time:" << duration.count() << "ms" << std::endl;

start_time = std::chrono::steady_clock::now();
tbb::parallel_for(0, kNumElements, [&](int i)
                  {  
                    tbb::concurrent_hash_map<int, int>::accessor acc;
                    map.find(acc,i); });
end_time = std::chrono::steady_clock::now();
duration = std::chrono::duration_cast<std::chrono::milliseconds>(end_time - start_time);
std::cout << GREEN << " -------------------find time:" << duration.count() << "ms" << std::endl;

额外问题与测试结果

是否存在比std::unordered_map更快的哈希表?已尝试ankerl/unordered_dense、tsl/robin_map,测试结果如下:

tsl::robin_map time:224ms
-------------------find time:21ms
tsl::robin_map multi-threads time:133ms
-------------------find time:17ms
ankerl::unordered_dense::map time:338ms
-------------------find time:270ms

尽管tsl::robin_map性能更优,但在自定义类中使用时速度大幅下降。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.25 22:07:38