map unordered_map hash_map的查找性能测试
結論如下:
Release模式下:
1. 容量為10的時候,查找效率:map > unordered_map > hash_map
2. 容量為100的時候,查找效率:map = unordered_map > hash_map
3. 容量為1000的時候,查找效率:unordered_map > hash_map > 4倍map
4. 容量為1萬的時候,查找效率:hash_map > unordered_map > 4倍map
5. 容量為10萬的時候,查找效率:hash_map > unordered_map > 4倍map
6. 容量為100萬的時候,查找效率:hash_map > unordered_map > 1.6倍map
7. 容量為1000萬的時候,查找效率:hash_map > unordered_map > 1.4倍map
8.?容量為1億的時候,程序崩潰了,哈哈哈哈哈哈,如果你知道原因請告訴我
9. debug模式下和release模式下,差距非常大,幾百倍的差距。。。。。
Debug模式下:
1. 查找效率:hash_map > unordered_map > map
2. 隨著容量的增加,hash_map, unordered_map的查找效率有所降低,但浮動不大畢竟是常量級別。map的效率直線下降。。。
3. 容量為一千萬的時候,程序同樣崩潰
實驗結果如下圖:
Release模式? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Debug模式(注意:相比Release模式還降低了10倍的查詢量)
? ??
?
代碼如下:
#include<iostream> #include<map> #include<hash_map> #include<unordered_map> #include<boost/progress.hpp>using namespace std; using namespace boost;// 測試函數, // size?? ??? ?:?? ?map的實際大小 // times?? ?:?? ?查找的輪次,每一輪次都從0查找到size-1 void test(int size, int times) {cout << "size=" << size << "; times=" << times << endl;map<int, int>?? ??? ??? ??? ?m;unordered_map<int, int>?? ??? ?um;hash_map<int, int>?? ??? ??? ?hm;// 初始化for (int i = 0; i<size; i++){m[i] = i;um[i] = i;hm[i] = i;}// map的查找{int count = 0;progress_timer t; // progress_timer變量會在創建時計時,析構時自動打印出耗時,所以不會受到前面初始化的影響,下同,不再解釋for (int i = 0; i<times; i++){for (int j = 0; j<size; j++){if (m.find(j) != m.end()){count++;}}}cout << "count:" << count <<", map:";}// unordered_map的查找{int count = 0;progress_timer t;for (int i = 0; i<times; i++){for (int j = 0; j<size; j++){if (um.find(j) != um.end()){count++;}}}cout << "count:" << count << ", unordered_map:";}// hash_map的查找{int count = 0;progress_timer t;for (int i = 0; i<times; i++){for (int j = 0; j<size; j++){if (hm.find(j) != hm.end()){count++;}}}cout << "count:" << count << ", hash_map:";} }int main() {test(10,10000000);?? ??? ?// 容量:10?? ??? ?查找:1千萬輪次test(100, 1000000);?? ??? ?// 容量:100?? ??? ?查找:1百萬輪次test(1000, 100000);?? ??? ?// 容量:1000?? ??? ?查找:10萬輪次test(10000, 10000);?? ??? ?// 容量:10000?? ?查找:1萬輪次test(100000, 1000);?? ??? ?// 容量:100000?? ?查找:1000輪次test(1000000, 100);?? ??? ?// 容量:1000000?? ?查找:100輪次test(10000000, 10);?? ??? ?// 容量:10000000?? ?查找:10輪次getchar();return 0; }?
總結
以上是生活随笔為你收集整理的map unordered_map hash_map的查找性能测试的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: c++读取csv文件示例
- 下一篇: c++实现的唯一ID生成器