Map 与 unordered_map 横向与纵向测试,附带原始数据与测试程序
Posted Icys
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Map 与 unordered_map 横向与纵向测试,附带原始数据与测试程序相关的知识,希望对你有一定的参考价值。
写程序时,面临用Map还是unordered_map,总是很纠结,于是写了个程序进行测试
Map 与 unordered_map 横向与纵向测试,附带原始数据与测试程序
配置
配件 | 信息 |
---|---|
CPU | Intel® Pentium(R) CPU G3260 @ 3.30GHz × 2 |
内存 | 7.6 GiB |
系统 | Ubuntu 20.04.1 LTS |
编译器 | g++-9 |
编译选项 | -O2 |
简单数据(4 Byte)
首先先上一组小数据
测试项目 | 数据范围 | 耗时(us) |
---|---|---|
Map写入 | 1000 | 94 |
——Map读取 | 1000 | 62 |
unordered_map写入 | 1000 | 101 |
——unordered_map读取 | 1000 | 61 |
可以看到数据较小时候是Map占优势
再来一组中等数据
测试项目 | 数据范围 | 耗时(us) |
---|---|---|
Map写入 | 10000 | 1682 |
——Map读取 | 10000 | 2703 |
unordered_map写入 | 10000 | 1160 |
——unordered_map读取 | 10000 | 677 |
大于1000后就是unordered_map优势更大
特大数据下情况是
测试项目 | 数据范围 | 耗时(us) |
---|---|---|
Map写入 | 1000000 | 855782 |
——Map读取 | 1000000 | 811670 |
unordered_map写入 | 1000000 | 267515 |
——unordered_map读取 | 1000000 | 118495 |
大数据便是unordered_map完全碾压
复杂数据(32 Byte)
有人可能会说当数据很复杂时unordered_map容易引发冲突
测试项目 | 数据范围 | 耗时(us) |
---|---|---|
Map写入 | 10000 | 4064 |
——Map读取 | 10000 | 4569 |
unordered_map写入 | 10000 | 2139 |
——unordered_map读取 | 10000 | 1716 |
和上面比较,发现数据变得更复杂时,Map更不如unordered_map了
重载和函数的速度
另外测试还发现,无论是写入还是读取,用重载方法都要比函数慢,其中读取慢的更多。
测试项目 | 数据范围 | 耗时(us) |
---|---|---|
Map重载写入 | 1000000 | 906477 |
——Map重载读取 | 1000000 | 1115637 |
Map函数写入 | 1000000 | 855782 |
——Map函数读取 | 1000000 | 811670 |
测试项目 | 数据范围 | 耗时(us) |
---|---|---|
unordered_map重载写入 | 1000000 | 284470 |
——unordered_map重载读取 | 1000000 | 276208 |
unordered_map函数写入 | 1000000 | 267515 |
——unordered_map函数读取 | 1000000 | 118495 |
总结
根据上面的数据可以分析出
在数据小于1000时,可以采取Map。大于1000,小于10000时,如果需要排序,选用Map,不需要就unordered_map。大于10000的时候没有特殊要求,采用Map。
另外,插入和读取如果有时间刚需,应该采用函数,而不是重载。
原始数据与测试程序
如果有人需要原始数据,可以看看。
int 4
map
operator
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <map>
using namespace std;
int main()
{
int start = 0;
for (int i = 1; i <= 10000; i++)
rand();
for (int n = 1; n <= 1e6; n *= 10)
for (int m = 1; m <= 1e6; m *= 10)
{
map<int, int> M;
srand(233);
start = clock();
for (int i = 1; i <= n; i++)
M[rand()] = rand();
cout << "|Writing spends |" << clock() - start << \'|\' << n << \'|\' << m << \'|\' << endl;
start = clock();
int temp;
for (int i = 1; i <= m; i++)
M[rand()];
cout << "|Reading spends |" << clock() - start << \'|\' << n << \'|\' << m << \'|\' << endl;
}
return 0;
}
项目 | 耗时(微秒) | 写入次数 | 读取次数 |
---|---|---|---|
Writing spends | 26 | 1 | 1 |
Reading spends | 3 | 1 | 1 |
Writing spends | 1 | 1 | 10 |
Reading spends | 2 | 1 | 10 |
Writing spends | 0 | 1 | 100 |
Reading spends | 15 | 1 | 100 |
Writing spends | 1 | 1 | 1000 |
Reading spends | 134 | 1 | 1000 |
Writing spends | 2 | 1 | 10000 |
Reading spends | 1781 | 1 | 10000 |
Writing spends | 4 | 1 | 100000 |
Reading spends | 48145 | 1 | 100000 |
Writing spends | 7 | 1 | 1000000 |
Reading spends | 823069 | 1 | 1000000 |
Writing spends | 7 | 10 | 1 |
Reading spends | 1 | 10 | 1 |
Writing spends | 1 | 10 | 10 |
Reading spends | 2 | 10 | 10 |
Writing spends | 1 | 10 | 100 |
Reading spends | 13 | 10 | 100 |
Writing spends | 1 | 10 | 1000 |
Reading spends | 217 | 10 | 1000 |
Writing spends | 2 | 10 | 10000 |
Reading spends | 2971 | 10 | 10000 |
Writing spends | 5 | 10 | 100000 |
Reading spends | 119565 | 10 | 100000 |
Writing spends | 9 | 10 | 1000000 |
Reading spends | 947049 | 10 | 1000000 |
Writing spends | 18 | 100 | 1 |
Reading spends | 1 | 100 | 1 |
Writing spends | 10 | 100 | 10 |
Reading spends | 2 | 100 | 10 |
Writing spends | 9 | 100 | 100 |
Reading spends | 17 | 100 | 100 |
Writing spends | 8 | 100 | 1000 |
Reading spends | 231 | 100 | 1000 |
Writing spends | 11 | 100 | 10000 |
Reading spends | 3457 | 100 | 10000 |
Writing spends | 14 | 100 | 100000 |
Reading spends | 86540 | 100 | 100000 |
Writing spends | 24 | 100 | 1000000 |
Reading spends | 1350311 | 100 | 1000000 |
Writing spends | 166 | 1000 | 1 |
Reading spends | 2 | 1000 | 1 |
Writing spends | 147 | 1000 | 10 |
Reading spends | 3 | 1000 | 10 |
Writing spends | 133 | 1000 | 100 |
Reading spends | 24 | 1000 | 100 |
Writing spends | 132 | 1000 | 1000 |
Reading spends | 245 | 1000 | 1000 |
Writing spends | 143 | 1000 | 10000 |
Reading spends | 3755 | 1000 | 10000 |
Writing spends | 200 | 1000 | 100000 |
Reading spends | 115802 | 1000 | 100000 |
Writing spends | 219 | 1000 | 1000000 |
Reading spends | 950769 | 1000 | 1000000 |
Writing spends | 2963 | 10000 | 1 |
Reading spends | 1 | 10000 | 1 |
Writing spends | 2988 | 10000 | 10 |
Reading spends | 6 | 10000 | 10 |
Writing spends | 2905 | 10000 | 100 |
Reading spends | 43 | 10000 | 100 |
Writing spends | 4573 | 10000 | 1000 |
Reading spends | 1448 | 10000 | 1000 |
Writing spends | 3492 | 10000 | 10000 |
Reading spends | 12881 | 10000 | 10000 |
Writing spends | 2880 | 10000 | 100000 |
Reading spends | 101483 | 10000 | 100000 |
Writing spends | 2951 | 10000 | 1000000 |
Reading spends | 935608 | 10000 | 1000000 |
Writing spends | 49535 | 100000 | 1 |
Reading spends | 2 | 100000 | 1 |
Writing spends | 70612 | 100000 | 10 |
Reading spends | 8 | 100000 | 10 |
Writing spends | 76232 | 100000 | 100 |
Reading spends | 76 | 100000 | 100 |
Writing spends | 80457 | 100000 | 1000 |
Reading spends | 790 | 100000 | 1000 |
Writing spends | 84432 | 100000 | 10000 |
Reading spends | 9677 | 100000 | 10000 |
Writing spends | 78376 | 100000 | 100000 |
Reading spends | 97320 | 100000 | 100000 |
Writing spends | 69574 | 100000 | 1000000 |
Reading spends | 965508 | 100000 | 1000000 |
Writing spends | 903613 | 1000000 | 1 |
Reading spends | 3 | 1000000 | 1 |
Writing spends | 907309 | 1000000 | 10 |
Reading spends | 12 | 1000000 | 10 |
Writing spends | 916617 | 1000000 | 100 |
Reading spends | 119 | 1000000 | 100 |
Writing spends | 917360 | 1000000 | 1000 |
Reading spends | 1177 | 1000000 | 1000 |
Writing spends | 904860 | 1000000 | 10000 |
Reading spends | 12960 | 1000000 | 10000 |
Writing spends | 905297 | 1000000 | 100000 |
Reading spends | 127472 | 1000000 | 100000 |
Writing spends | 906477 | 1000000 | 1000000 |
Reading spends | 1115637 | 1000000 | 1000000 |
insert & find
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <map>
using namespace std;
int main()
{
int start = 0;
for (int i = 1; i <= 10000; i++)
rand();
for (int n = 1; n <= 1e6; n *= 10)
for (int m = 1; m <= 1e6; m *= 10)
{
map<int, int> M;
srand(233);
start = clock();
for (int i = 1; i <= n; i++)
M.insert(make_pair(rand(), rand()));
cout << "|Writing spends |" << clock() - start << \'|\' << n << \'|\' << m << \'|\' << endl;
start = clock();
int temp;
for (int i = 1; i <= m; i++)
M.find(rand());
cout << "|Reading spends |" << clock() - start << \'|\' << n << \'|\' << m << \'|\' << endl;
}
return 0;
}
项目 | 耗时(微秒) | 写入次数 | 读取次数 |
---|---|---|---|
Writing spends | 26 | 1 | 1 |
Reading spends | 1 | 1 | 1 |
Writing spends | 1 | 1 | 10 |
Reading spends | 1 | 1 | 10 |
Writing spends | 0 | 1 | 100 |
Reading spends | 2 | 1 | 100 |
Writing spends | 0 | 1 | 1000 |
Reading spends | 12 | 1 | 1000 |
Writing spends | 1 | 1 | 10000 |
Reading spends | 109 | 1 | 10000 |
Writing spends | 1 | 1 | 100000 |
Reading spends | 1086 | 1 | 100000 |
Writing spends | 1 | 1 | 1000000 |
Reading spends | 10757 | 1 | 1000000 |
Writing spends | 3 | 10 | 1 |
Reading spends | 1 | 10 | 1 |
Writing spends | 2 | 10 | 10 |
Reading spends | 1 | 10 | 10 |
Writing spends | 1 | 10 | 100 |
Reading spends | 3 | 10 | 100 |
Writing spends | 1 | 10 | 1000 |
Reading spends | 19 | 10 | 1000 |
Writing spends | 1 | 10 | 10000 |
Reading spends | 187 | 10 | 10000 |
Writing spends | 1 | 10 | 100000 |
Reading spends | 1813 | 10 | 100000 |
Writing spends | 2 | 10 | 1000000 |
Reading spends | 18350 | 10 | 1000000 |
Writing spends | 19 | 100 | 1 |
Reading spends | 4 | 100 | 1 |
Writing spends | 12 | 100 | 10 |
Reading spends | 1 | 100 | 10 |
Writing spends | 7 | 100 | 100 |
Reading spends | 5 | 100 | 100 |
Writing spends | 7 | 100 | 1000 |
Reading spends | 37 | 100 | 1000 |
Writing spends | 7 | 100 | 10000 |
Reading spends | 367 | 100 | 10000 |
Writing spends | 11 | 100 | 100000 |
Reading spends | 3599 | 100 | 100000 |
Writing spends | 11 | 100 | 1000000 |
Reading spends | 35590 | 100 | 1000000 |
Writing spends | 259 | 1000 | 1 |
Reading spends | 2 | 1000 | 1 |
Writing spends | 107 | 1000 | 10 |
Reading spends | 2 | 1000 | 10 |
Writing spends | 99 | 1000 | 100 |
Reading spends | 9 | 1000 | 100 |
Writing spends | 94 | 1000 | 1000 |
Reading spends | 62 | 1000 | 1000 |
Writing spends | 95 | 1000 | 10000 |
Reading spends | 565 | 1000 | 10000 |
Writing spends | 99 | 1000 | 100000 |
Reading spends | 5584 | 1000 | 100000 |
Writing spends | 101 | 1000 | 1000000 |
Reading spends | 55971 | 1000 | 1000000 |
Writing spends | 1732 | 10000 | 1 |
Reading spends | 1 | 10000 | 1 |
Writing spends | 2230 | 10000 | 10 |
Reading spends | 4 | 10000 | 10 |
Writing spends | 2644 | 10000 | 100 |
Reading spends | 28 | 10000 | 100 |
Writing spends | 1633 | 10000 | 1000 |
Reading spends | 188 | 10000 | 1000 |
Writing spends | 1682 | 10000 | 10000 |
Reading spends | 2703 | 10000 | 10000 |
Writing spends | 1638 | 10000 | 100000 |
Reading spends | 14327 | 10000 | 100000 |
Writing spends | 1546 | 10000 | 1000000 |
Reading spends | 142330 | 10000 | 1000000 |
Writing spends | 67350 | 100000 | 1 |
Reading spends | 2 | 100000 | 1 |
Writing spends | 69654 | 100000 | 10 |
Reading spends | 4 | 100000 | 10 |
Writing spends | 64137 | 100000 | 100 |
Reading spends | 39 | 100000 | 100 |
Writing spends | 67940 | 100000 | 1000 |
Reading spends | 436 | 100000 | 1000 |
Writing spends | 63069 | 100000 | 10000 |
Reading spends | 4709 | 100000 | 10000 |
Writing spends | 86477 | 100000 | 100000 |
Reading spends | 64116 | 100000 | 100000 |
Writing spends | 67929 | 100000 | 1000000 |
Reading spends | 370789 | 100000 | 1000000 |
Writing spends | 768626 | 1000000 | 1 |
Reading spends | 2 | 1000000 | 1 |
Writing spends | 850479 | 1000000 | 10 |
Reading spends | 12 | 1000000 | 10 |
Writing spends | 857980 | 1000000 | 100 |
Reading spends | 84 | 1000000 | 100 |
Writing spends | 854080 | 1000000 | 1000 |
Reading spends | 899 | 1000000 | 1000 |
Writing spends | 856615 | 1000000 | 10000 |
Reading spends | 9960 | 1000000 | 10000 |
Writing spends | 871467 | 1000000 | 100000 |
Reading spends | 111029 | 1000000 | 100000 |
Writing spends | 855782 | 1000000 | 1000000 |
Reading spends | 811670 | 1000000 | 1000000 |
unordered_map
operator
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <tr1/unordered_map>
using namespace std;
using namespace tr1;
int main()
{
int start = 0;
for (int i = 1; i <= 10000; i++)
rand();
for (int n = 1; n <= 1e6; n *= 10)
for (int m = 1; m <= 1e6; m *= 10)
{
unordered_map<int, int> M;
srand(233);
start = clock();
for (int i = 1; i <= n; i++)
M[rand()] = rand();
cout << "|Writing spends |" << clock() - start << \'|\' << n << \'|\' << m << \'|\' << endl;
start = clock();
int temp;
for (int i = 1; i <= m; i++)
M[rand()];
cout << "|Reading spends |" << clock() - start << \'|\' << n << \'|\' << m << \'|\' << endl;
}
return 0;
}
项目 | 耗时(微秒) | 写入次数 | 读取次数 |
---|---|---|---|
Writing spends | 22 | 1 | 1 |
Reading spends | 3 | 1 | 1 |
Writing spends | 1 | 1 | 10 |
Reading spends | 2 | 1 | 10 |
Writing spends | 1 | 1 | 100 |
Reading spends | 15 | 1 | 100 |
Writing spends | 2 | 1 | 1000 |
Reading spends | 98 | 1 | 1000 |
Writing spends | 2 | 1 | 10000 |
Reading spends | 973 | 1 | 10000 |
Writing spends | 2 | 1 | 100000 |
Reading spends | 18905 | 1 | 100000 |
Writing spends | 7 | 1 | 1000000 |
Reading spends | 304928 | 1 | 1000000 |
Writing spends | 6 | 10 | 1 |
Reading spends | 0 | 10 | 1 |
Writing spends | 1 | 10 | 10 |
Reading spends | 2 | 10 | 10 |
Writing spends | 1 | 10 | 100 |
Reading spends | 211558 | 10 | 100 |
Writing spends | 2 | 10 | 1000 |
Reading spends | 83 | 10 | 1000 |
Writing spends | 1 | 10 | 10000 |
Reading spends | 816 | 10 | 10000 |
Writing spends | 2 | 10 | 100000 |
Reading spends | 18775 | 10 | 100000 |
Writing spends | 9 | 10 | 1000000 |
Reading spends | 331476 | 10 | 1000000 |
Writing spends | 18 | 100 | 1 |
Reading spends | 1 | 100 | 1 |
Writing spends | 10 | 100 | 10 |
Reading spends | 2 | 100 | 10 |
Writing spends | 9 | 100 | 100 |
Reading spends | 10 | 100 | 100 |
Writing spends | 10 | 100 | 1000 |
Reading spends | 84 | 100 | 1000 |
Writing spends | 21 | 100 | 10000 |
Reading spends | 856 | 100 | 10000 |
Writing spends | 16 | 100 | 100000 |
Reading spends | 19406 | 100 | 100000 |
Writing spends | 20 | 100 | 1000000 |
Reading spends | 344753 | 100 | 1000000 |
Writing spends | 93 | 1000 | 1 |
Reading spends | 1 | 1000 | 1 |
Writing spends | 98 | 1000 | 10 |
Reading spends | 1 | 1000 | 10 |
Writing spends | 95 | 1000 | 100 |
Reading spends | 5 | 1000 | 100 |
Writing spends | 94 | 1000 | 1000 |
Reading spends | 81 | 1000 | 1000 |
Writing spends | 106 | 1000 | 10000 |
Reading spends | 749 | 1000 | 10000 |
Writing spends | 97 | 1000 | 100000 |
Reading spends | 20981 | 1000 | 100000 |
Writing spends | 110 | 1000 | 1000000 |
Reading spends | 330149 | 1000 | 1000000 |
Writing spends | 858 | 10000 | 1 |
Reading spends | 1 | 10000 | 1 |
Writing spends | 858 | 10000 | 10 |
Reading spends | 1 | 10000 | 10 |
Writing spends | 837 | 10000 | 100 |
Reading spends | 7 | 10000 | 100 |
Writing spends | 841 | 10000 | 1000 |
Reading spends | 59 | 10000 | 1000 |
Writing spends | 850 | 10000 | 10000 |
Reading spends | 950 | 10000 | 10000 |
Writing spends | 849 | 10000 | 100000 |
Reading spends | 27480 | 10000 | 100000 |
Writing spends | 919 | 10000 | 1000000 |
Reading spends | 338245 | 10000 | 1000000 |
Writing spends | 33755 | 100000 | 1 |
Reading spends | 1 | 100000 | 1 |
Writing spends | 29546 | 100000 | 10 |
Reading spends | 3 | 100000 | 10 |
Writing spends | 31192 | 100000 | 100 |
Reading spends | 97 | 100000 | 100 |
Writing spends | 30126 | 100000 | 1000 |
Reading spends | 216 | 100000 | 1000 |
Writing spends | 37193 | 100000 | 10000 |
Reading spends | 1996 | 100000 | 10000 |
Writing spends | 32065 | 100000 | 100000 |
Reading spends | 39717 | 100000 | 100000 |
Writing spends | 17520 | 100000 | 1000000 |
Reading spends | 473755 | 100000 | 1000000 |
Writing spends | 480733 | 1000000 | 1 |
Reading spends | 2 | 1000000 | 1 |
Writing spends | 345884 | 1000000 | 10 |
Reading spends | 44 | 1000000 | 10 |
Writing spends | 284269 | 1000000 | 100 |
Reading spends | 24 | 1000000 | 100 |
Writing spends | 283844 | 1000000 | 1000 |
Reading spends | 223 | 1000000 | 1000 |
Writing spends | 278945 | 1000000 | 10000 |
Reading spends | 2068 | 1000000 | 10000 |
Writing spends | 280158 | 1000000 | 100000 |
Reading spends | 189937 | 1000000 | 100000 |
Writing spends | 284470 | 1000000 | 1000000 |
Reading spends | 376208 | 1000000 | 1000000 |
insert & find
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <tr1/unordered_map>
using namespace std;
using namespace tr1;
int main()
{
int start = 0;
for (int i = 1; i <= 10000; i++)
rand();
for (int n = 1; n <= 1e6; n *= 10)
for (int m = 1; m <= 1e6; m *= 10)
{
unordered_map<int, int> M;
srand(233);
start = clock();
for (int i = 1; i <= n; i++)
M.insert(make_pair(rand(), rand()));
cout << "|Writing spends |" << clock() - start << \'|\' << n << \'|\' << m << \'|\' << endl;
start = clock();
int temp;
for (int i = 1; i <= m; i++)
M.find(rand());
cout << "|Reading spends |" << clock() - start << \'|\' << n << \'|\' << m << \'|\' << endl;
}
return 0;
}
项目 | 耗时(微秒) | 写入次数 | 读取次数 |
---|---|---|---|
Writing spends | 24 | 1 | 1 |
Reading spends | 1 | 1 | 1 |
Writing spends | 1 | 1 | 10 |
Reading spends | 1 | 1 | 10 |
Writing spends | 1 | 1 | 100 |
Reading spends | 2 | 1 | 100 |
Writing spends | 1 | 1 | 1000 |
Reading spends | 18 | 1 | 1000 |
Writing spends | 1 | 1 | 10000 |
Reading spends | 174 | 1 | 10000 |
Writing spends | 1 | 1 | 100000 |
Reading spends | 1720 | 1 | 100000 |
Writing spends | 1 | 1 | 1000000 |
Reading spends | 17160 | 1 | 1000000 |
Writing spends | 2 | 10 | 1 |
Reading spends | 1 | 10 | 1 |
Writing spends | 1 | 10 | 10 |
Reading spends | 1 | 10 | 10 |
Writing spends | 1 | 10 | 100 |
Reading spends | 4 | 10 | 100 |
Writing spends | 1 | 10 | 1000 |
Reading spends | 25 | 10 | 1000 |
Writing spends | 1 | 10 | 10000 |
Reading spends | 238 | 10 | 10000 |
Writing spends | 1 | 10 | 100000 |
Reading spends | 2361 | 10 | 100000 |
Writing spends | 1 | 10 | 1000000 |
Reading spends | 23766 | 10 | 1000000 |
Writing spends | 18 | 100 | 1 |
Reading spends | 6 | 100 | 1 |
Writing spends | 12 | 100 | 10 |
Reading spends | 4 | 100 | 10 |
Writing spends | 13 | 100 | 100 |
Reading spends | 13 | 100 | 100 |
Writing spends | 17 | 100 | 1000 |
Reading spends | 45 | 100 | 1000 |
Writing spends | 14 | 100 | 10000 |
Reading spends | 251 | 100 | 10000 |
Writing spends | 12 | 100 | 100000 |
Reading spends | 2390 | 100 | 100000 |
Writing spends | 12 | 100 | 1000000 |
Reading spends | 24286 | 100 | 1000000 |
Writing spends | 126 | 1000 | 1 |
Reading spends | 1 | 1000 | 1 |
Writing spends | 98 | 1000 | 10 |
Reading spends | 1 | 1000 | 10 |
Writing spends | 93 | 1000 | 100 |
Reading spends | 3 | 1000 | 100 |
Writing spends | 101 | 1000 | 1000 |
Reading spends | 61 | 1000 | 1000 |
Writing spends | 104 | 1000 | 10000 |
Reading spends | 259 | 1000 | 10000 |
Writing spends | 97 | 1000 | 100000 |
Reading spends | 2492 | 1000 | 100000 |
Writing spends | 96 | 1000 | 1000000 |
Reading spends | 25507 | 1000 | 1000000 |
Writing spends | 1587 | 10000 | 1 |
Reading spends | 1 | 10000 | 1 |
Writing spends | 1013 | 10000 | 10 |
Reading spends | 1 | 10000 | 10 |
Writing spends | 823 | 10000 | 100 |
Reading spends | 8 | 10000 | 100 |
Writing spends | 840 | 10000 | 1000 |
Reading spends | 54 | 10000 | 1000 |
Writing spends | 1160 | 10000 | 10000 |
Reading spends | 677 | 10000 | 10000 |
Writing spends | 813 | 10000 | 100000 |
Reading spends | 4968 | 10000 | 100000 |
Writing spends | 808 | 10000 | 1000000 |
Reading spends | 38649 | 10000 | 1000000 |
Writing spends | 28790 | 100000 | 1 |
Reading spends | 11 | 100000 | 1 |
Writing spends | 45300 | 100000 | 10 |
Reading spends | 5 | 100000 | 10 |
Writing spends | 34463 | 100000 | 100 |
Reading spends | 14 | 100000 | 100 |
Writing spends | 35388 | 100000 | 1000 |
Reading spends | 113 | 100000 | 1000 |
Writing spends | 32767 | 100000 | 10000 |
Reading spends | 2677 | 100000 | 10000 |
Writing spends | 31524 | 100000 | 100000 |
Reading spends | 13741 | 100000 | 100000 |
Writing spends | 33473 | 100000 | 1000000 |
Reading spends | 100586 | 100000 | 1000000 |
Writing spends | 300962 | 1000000 | 1 |
Reading spends | 1 | 1000000 | 1 |
Writing spends | 442759 | 1000000 | 10 |
Reading spends | 2 | 1000000 | 10 |
Writing spends | 270830 | 1000000 | 100 |
Reading spends | 10 | 1000000 | 100 |
Writing spends | 270029 | 1000000 | 1000 |
Reading spends | 124 | 1000000 | 1000 |
Writing spends | 268652 | 1000000 | 10000 |
Reading spends | 1192 | 1000000 | 10000 |
Writing spends | 269445 | 1000000 | 100000 |
Reading spends | 12717 | 1000000 | 100000 |
Writing spends | 267515 | 1000000 | 1000000 |
Reading spends | 118495 | 1000000 | 1000000 |
Btye 32
map
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <map>
using namespace std;
inline __uint128_t rand128()
{
return rand() | ((__uint128_t)rand() << 32) | ((__uint128_t)rand() << 64) | ((__uint128_t)rand() << 96);
}
int main()
{
int start = 0;
for (int i = 1; i <= 10000; i++)
rand();
for (int n = 1; n <= 1e6; n *= 10)
for (int m = 1; m <= 1e6; m *= 10)
{
map<pair<__uint128_t, __uint128_t>, int> M;
srand(233);
start = clock();
for (int i = 1; i <= n; i++)
M.insert(make_pair(make_pair(rand128(), rand128()), rand()));
cout << "|Writing spends |" << clock() - start << \'|\' << n << \'|\' << m << \'|\' << endl;
start = clock();
int temp;
for (int i = 1; i <= m; i++)
M.find(make_pair(rand128(), rand128()));
cout << "|Reading spends |" << clock() - start << \'|\' << n << \'|\' << m << \'|\' << endl;
}
return 0;
}
项目 | 耗时(微秒) | 写入次数 | 读取次数 |
---|---|---|---|
Writing spends | 24 | 1 | 1 |
Reading spends | 0 | 1 | 1 |
Writing spends | 1 | 1 | 10 |
Reading spends | 2 | 1 | 10 |
Writing spends | 1 | 1 | 100 |
Reading spends | 9 | 1 | 100 |
Writing spends | 1 | 1 | 1000 |
Reading spends | 70 | 1 | 1000 |
Writing spends | 1 | 1 | 10000 |
Reading spends | 693 | 1 | 10000 |
Writing spends | 1 | 1 | 100000 |
Reading spends | 7061 | 1 | 100000 |
Writing spends | 2 | 1 | 1000000 |
Reading spends | 70166 | 1 | 1000000 |
Writing spends | 4 | 10 | 1 |
Reading spends | 1 | 10 | 1 |
Writing spends | 3 | 10 | 10 |
Reading spends | 2 | 10 | 10 |
Writing spends | 1 | 10 | 100 |
Reading spends | 9 | 10 | 100 |
Writing spends | 1 | 10 | 1000 |
Reading spends | 83 | 10 | 1000 |
Writing spends | 2 | 10 | 10000 |
Reading spends | 802 | 10 | 10000 |
Writing spends | 2 | 10 | 100000 |
Reading spends | 7936 | 10 | 100000 |
Writing spends | 3 | 10 | 1000000 |
Reading spends | 81096 | 10 | 1000000 |
Writing spends | 26 | 100 | 1 |
Reading spends | 2 | 100 | 1 |
Writing spends | 18 | 100 | 10 |
Reading spends | 3 | 100 | 10 |
Writing spends | 18 | 100 | 100 |
Reading spends | 13 | 100 | 100 |
Writing spends | 18 | 100 | 1000 |
Reading spends | 103 | 100 | 1000 |
Writing spends | 17 | 100 | 10000 |
Reading spends | 996 | 100 | 10000 |
Writing spends | 20 | 100 | 100000 |
Reading spends | 9949 | 100 | 100000 |
Writing spends | 19 | 100 | 1000000 |
Reading spends | 100397 | 100 | 1000000 |
Writing spends | 226 | 1000 | 1 |
Reading spends | 1 | 1000 | 1 |
Writing spends | 166 | 1000 | 10 |
Reading spends | 3 | 1000 | 10 |
Writing spends | 164 | 1000 | 100 |
Reading spends | 15 | 1000 | 100 |
Writing spends | 164 | 1000 | 1000 |
Reading spends | 133 | 1000 | 1000 |
Writing spends | 167 | 1000 | 10000 |
Reading spends | 1278 | 1000 | 10000 |
Writing spends | 168 | 1000 | 100000 |
Reading spends | 12895 | 1000 | 100000 |
Writing spends | 174 | 1000 | 1000000 |
Reading spends | 127273 | 1000 | 1000000 |
Writing spends | 2917 | 10000 | 1 |
Reading spends | 2 | 10000 | 1 |
Writing spends | 2730 | 10000 | 10 |
Reading spends | 4 | 10000 | 10 |
Writing spends | 3344 | 10000 | 100 |
Reading spends | 36 | 10000 | 100 |
Writing spends | 5555 | 10000 | 1000 |
Reading spends | 670 | 10000 | 1000 |
Writing spends | 4300 | 10000 | 10000 |
Reading spends | 4064 | 10000 | 10000 |
Writing spends | 4569 | 10000 | 100000 |
Reading spends | 35972 | 10000 | 100000 |
Writing spends | 3696 | 10000 | 1000000 |
Reading spends | 235334 | 10000 | 1000000 |
Writing spends | 80213 | 100000 | 1 |
Reading spends | 1 | 100000 | 1 |
Writing spends | 75975 | 100000 | 10 |
Reading spends | 7 | 100000 | 10 |
Writing spends | 72787 | 100000 | 100 |
Reading spends | 61 | 100000 | 100 |
Writing spends | 72727 | 100000 | 1000 |
Reading spends | 620 | 100000 | 1000 |
Writing spends | 78175 | 100000 | 10000 |
Reading spends | 6618 | 100000 | 10000 |
Writing spends | 85278 | 100000 | 100000 |
Reading spends | 74804 | 100000 | 100000 |
Writing spends | 69928 | 100000 | 1000000 |
Reading spends | 541858 | 100000 | 1000000 |
Writing spends | 937039 | 1000000 | 1 |
Reading spends | 2 | 1000000 | 1 |
Writing spends | 996383 | 1000000 | 10 |
Reading spends | 12 | 1000000 | 10 |
Writing spends | 997665 | 1000000 | 100 |
Reading spends | 116 | 1000000 | 100 |
Writing spends | 1001405 | 1000000 | 1000 |
Reading spends | 1115 | 1000000 | 1000 |
Writing spends | 994444 | 1000000 | 10000 |
Reading spends | 11241 | 1000000 | 10000 |
Writing spends | 1009323 | 1000000 | 100000 |
Reading spends | 126125 | 1000000 | 100000 |
Writing spends | 998410 | 1000000 | 1000000 |
Reading spends | 1002439 | 1000000 | 1000000 |
unordered_map
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <functional>
#include <tr1/unordered_map>
using namespace std;
using namespace tr1;
struct hashfunc
{
inline size_t operator()(const pair<__uint128_t, __uint128_t> &i) const
{
return tr1::hash<long long>()((long long)i.first) ^ tr1::hash<long long>()((long long)(i.first >> 64)) ^ tr1::hash<long long>()((long long)i.second) ^ tr1::hash<long long>()((long long)(i.second >> 64));
}
};
inline __uint128_t rand128()
{
return rand() | ((__uint128_t)rand() << 32) | ((__uint128_t)rand() << 64) | ((__uint128_t)rand() << 96);
}
int main()
{
int start = 0;
for (int i = 1; i <= 10000; i++)
rand();
for (int n = 1; n <= 1e6; n *= 10)
for (int m = 1; m <= 1e6; m *= 10)
{
unordered_map<pair<__uint128_t, __uint128_t>, int, hashfunc> M;
srand(233);
start = clock();
for (int i = 1; i <= n; i++)
M.insert(make_pair(make_pair(rand128(), rand128()), rand()));
cout << "|Writing spends |" << clock() - start << \'|\' << n << \'|\' << m << \'|\' << endl;
start = clock();
int temp;
for (int i = 1; i <= m; i++)
M.find(make_pair(rand128(), rand128()));
cout << "|Reading spends |" << clock() - start << \'|\' << n << \'|\' << m << \'|\' << endl;
}
return 0;
}
项目 | 耗时(微秒) | 写入次数 | 读取次数 |
---|---|---|---|
Writing spends | 21 | 1 | 1 |
Reading spends | 2 | 1 | 1 |
Writing spends | 1 | 1 | 10 |
Reading spends | 1 | 1 | 10 |
Writing spends | 1 | 1 | 100 |
Reading spends | 9 | 1 | 100 |
Writing spends | 1 | 1 | 1000 |
Reading spends | 76 | 1 | 1000 |
Writing spends | 1 | 1 | 10000 |
Reading spends | 744 | 1 | 10000 |
Writing spends | 1 | 1 | 100000 |
Reading spends | 7466 | 1 | 100000 |
Writing spends | 2 | 1 | 1000000 |
Reading spends | 74453 | 1 | 1000000 |
Writing spends | 3 | 10 | 1 |
Reading spends | 0 | 10 | 1 |
Writing spends | 2 | 10 | 10 |
Reading spends | 2 | 10 | 10 |
Writing spends | 2 | 10 | 100 |
Reading spends | 9 | 10 | 100 |
Writing spends | 2 | 10 | 1000 |
Reading spends | 85 | 10 | 1000 |
Writing spends | 2 | 10 | 10000 |
Reading spends | 836 | 10 | 10000 |
Writing spends | 1 | 10 | 100000 |
Reading spends | 8370 | 10 | 100000 |
Writing spends | 2 | 10 | 1000000 |
Reading spends | 84176 | 10 | 1000000 |
Writing spends | 27 | 100 | 1 |
Reading spends | 1 | 100 | 1 |
Writing spends | 17 | 100 | 10 |
Reading spends | 2 | 100 | 10 |
Writing spends | 15 | 100 | 100 |
Reading spends | 9 | 100 | 100 |
Writing spends | 14 | 100 | 1000 |
Reading spends | 83 | 100 | 1000 |
Writing spends | 14 | 100 | 10000 |
Reading spends | 825 | 100 | 10000 |
Writing spends | 15 | 100 | 100000 |
Reading spends | 8127 | 100 | 100000 |
Writing spends | 20 | 100 | 1000000 |
Reading spends | 81937 | 100 | 1000000 |
Writing spends | 211 | 1000 | 1 |
Reading spends | 1 | 1000 | 1 |
Writing spends | 164 | 1000 | 10 |
Reading spends | 2 | 1000 | 10 |
Writing spends | 143 | 1000 | 100 |
Reading spends | 9 | 1000 | 100 |
Writing spends | 160 | 1000 | 1000 |
Reading spends | 84 | 1000 | 1000 |
Writing spends | 144 | 1000 | 10000 |
Reading spends | 839 | 1000 | 10000 |
Writing spends | 161 | 1000 | 100000 |
Reading spends | 8344 | 1000 | 100000 |
Writing spends | 161 | 1000 | 1000000 |
Reading spends | 84518 | 1000 | 1000000 |
Writing spends | 2609 | 10000 | 1 |
Reading spends | 1 | 10000 | 1 |
Writing spends | 2341 | 10000 | 10 |
Reading spends | 8 | 10000 | 10 |
Writing spends | 1959 | 10000 | 100 |
Reading spends | 18 | 10000 | 100 |
Writing spends | 1548 | 10000 | 1000 |
Reading spends | 193 | 10000 | 1000 |
Writing spends | 2139 | 10000 | 10000 |
Reading spends | 1716 | 10000 | 10000 |
Writing spends | 1929 | 10000 | 100000 |
Reading spends | 14391 | 10000 | 100000 |
Writing spends | 1573 | 10000 | 1000000 |
Reading spends | 128671 | 10000 | 1000000 |
Writing spends | 37911 | 100000 | 1 |
Reading spends | 1 | 100000 | 1 |
Writing spends | 64046 | 100000 | 10 |
Reading spends | 3 | 100000 | 10 |
Writing spends | 29753 | 100000 | 100 |
Reading spends | 18 | 100000 | 100 |
Writing spends | 24535 | 100000 | 1000 |
Reading spends | 211 | 100000 | 1000 |
Writing spends | 34458 | 100000 | 10000 |
Reading spends | 1982 | 100000 | 10000 |
Writing spends | 41840 | 100000 | 100000 |
Reading spends | 30702 | 100000 | 100000 |
Writing spends | 44858 | 100000 | 1000000 |
Reading spends | 195389 | 100000 | 1000000 |
Writing spends | 426814 | 1000000 | 1 |
Reading spends | 2 | 1000000 | 1 |
Writing spends | 637874 | 1000000 | 10 |
Reading spends | 3 | 1000000 | 10 |
Writing spends | 405023 | 1000000 | 100 |
Reading spends | 28 | 1000000 | 100 |
Writing spends | 388910 | 1000000 | 1000 |
Reading spends | 301 | 1000000 | 1000 |
Writing spends | 402672 | 1000000 | 10000 |
Reading spends | 2515 | 1000000 | 10000 |
Writing spends | 393968 | 1000000 | 100000 |
Reading spends | 29590 | 1000000 | 100000 |
Writing spends | 395403 | 1000000 | 1000000 |
Reading spends | 250795 | 1000000 | 1000000 |
以上是关于Map 与 unordered_map 横向与纵向测试,附带原始数据与测试程序的主要内容,如果未能解决你的问题,请参考以下文章