问题描述
大家好,
考虑到插入和查找,我只是想知道这些数据结构中哪个具有更好的性能.从一些文章和论坛帖子中,我可以了解到unordered_map具有更好的性能.
I just want to know which of these data structure has better performance considering the insert and find. From some of the articles and forum posts I can understand that unordered_map has better performance.
但是当我使用V型放大器对它进行测试时,我发现并没有太大的差异.
But when I tested it using V-tune amplifier I don't find lot of differences.
我正在插入4000万个要映射的对象,键值的类型为< long long,指向对象的指针-64位>.以下是用于测试的代码段.
I am inserting 40 million objects to map, key-value has type of <long long, Pointer to object - 64 bit>. Following are the code snippets used for testing.
法线贴图:
for(long long n = 0; n< nCount; n ++)
{
testMap.insert(std :: make_pair(n,pObject));
}
for (long long n = 0; n < nCount; n++)
{
testMap.insert(std::make_pair(n, pObject));
}
Unorderd_map插入:
Unorderd_map inserts:
两者都占用了将近22到23秒的CPU时间.我可以看到负载计数(V-Tune放大器计数器)的显着差异.法线贴图的加载量为59.4亿,unordered_map的加载量为13.38亿.但考虑到内存限制,法线贴图为48.8% unodered_map是80.9%.我认为即使在无序映射的情况下负载也更少,它更多的是受内存限制的应用程序,因此最终两者都具有相同的性能.
Both are taking almost 22 to 23 seconds of cpu time. I can see significant difference in load count (V-Tune amplifier counter) . Normal map has 5940 million loads and unordered_map has 1338 million loads. But considering the memory bound, normal map is 48.8% and unodered_map is 80.9%. I think even the loads are less in case of unordered map it is more a memory bound application, so finally both become same kind of performance.
测试结果是在相同负载和相同环境下5次尝试的平均值.我正在使用Visual C ++ 2015、64位和Windows 7.
Test results are the average of 5 attempts in same load and same environment. I am using Visual C++ 2015, 64 bit and windows 7.
从我所有的测试中,我无法得出结论,专家可以请您帮助我找到结论.哪个是更好的法线贴图或unordered_map.
from all my tests, I cannot come to a conclusion, experts could you please help me to find a conclusion. Which is better normal map or unordered_map.
感谢&问候
补充
推荐答案
我怀疑,通过对键进行排序,您可能正在为std :: map行使最佳情况,而不是一般情况.尝试先随机改组密钥.
I suspect that, by having the keys ordered, you may be exercising the best case for std::map, rather than the average case. Try randomly shuffling the keys first.
这篇关于std :: unordered_map或普通std :: map具有更好的性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!