关于c++ STL map 和 unordered_map 的效率的对比测试
本文采用在随机读取和插入的情况下测试map和unordered_map的效率
笔者的电脑是台渣机,现给出配置信息
处理器 : Intel Pentium(R) CPU G850 @ 2.90GHz × 2
内存 : 7.7GiB
操作系统 : Ubuntu 20.04.2 LTS 64位(Noi Linux 2.0)
由于在数据量小的情况下二者时间差异微乎其微,测试范围从1e4开始到1e7,map unordered_map存储为 int, int二元组,int三元组
单 int测试
| 插入范围 | 查询范围 | map插入时间 | map查询时间 | unordered_map插入时间 | unordered_map查询时间 |
|---|---|---|---|---|---|
| 10000 | 10000 | 11.95ms | 10.268ms | 15.184ms | 5.009ms |
| 100000 | 100000 | 113.374ms | 134.365ms | 200.852ms | 79.029ms |
| 1000000 | 1000000 | 1606.66ms | 1819.2ms | 2603.46ms | 862.877ms |
| 10000000 | 10000000 | 21688.5ms | 25461.7ms | 33973.7ms | 9484.45ms |
#include <bits/stdc++.h>
using namespace std;
int main()
{
for(int n = 1e4, m = 1e4; n <= 1e9, m <= 1e9; n *= 10,m *= 10)
{
cout << n << "|" << m ;
srand(n);
int t0 = clock();
map <int ,int> M;
for(int i = 1; i <= n; i++)
{
M[rand()] = i;
}
cout << "| " << (double)(clock() - t0) / 1000 << "ms ";
unsigned sum = 0;
t0 = clock();
for(int i = 1; i <= m; i++)
{
sum += M[rand()];
}
cout << "| " << (double)(clock() - t0) / 1000 << "ms ";
srand(n);
unordered_map <int, int> U;
for(int i = 1; i <= n; i++)
{
U[rand()] = i;
}
cout << "| " << (double)(clock() - t0) / 1000 << "ms ";
sum = 0;
t0 = clock();
for(int i = 1; i <= m; i++)
{
sum += U[rand()];
}
cout << "| " << (double)(clock() - t0) / 1000 << "ms |" << endl;
}
return 0;
}
二元组测试
| 插入范围 | 查询范围 | map插入时间 | map查询时间 | unordered_map插入时间 | unordered_map查询时间 |
|---|---|---|---|---|---|
| 10000 | 10000 | 10.617ms | 11.523ms | 16.301ms | 5.14ms |
| 100000 | 100000 | 122.489ms | 141.413ms | 195.278ms | 70.122ms |
| 1000000 | 1000000 | 1792.69ms | 2173.48ms | 2879.21ms | 783.437ms |
| 10000000 | 10000000 | 25017.8ms | 28777.1ms | 36499.8ms | 8666.73ms |
#include <bits/stdc++.h>
using namespace std;
struct PII
{
int x, y;
bool operator ==(const PII b) const
{
return x == b.x && y == b.y;
}
bool operator <(const PII b) const
{
return x != b.x ? x < b.x : y < b.y;
}
};
struct hashPII
{
size_t operator()(const PII &p) const
{
return hash<int>()(p.x) ^ hash<int>()(p.y);
}
};
int main()
{
for(int n = 1e4, m = 1e4; n <= 1e9, m <= 1e9; n *= 10,m *= 10)
{
cout << n << "|" << m ;
srand(n);
int t0 = clock();
map <PII ,int> M;
for(int i = 1; i <= n; i++)
{
M[{rand(), rand()}] = i;
}
cout << "| " << (double)(clock() - t0) / 1000 << "ms ";
unsigned sum = 0;
t0 = clock();
for(int i = 1; i <= m; i++)
{
sum += M[{rand(), rand()}];
}
cout << "| " << (double)(clock() - t0) / 1000 << "ms ";
srand(n);
unordered_map <PII, int, hashPII> U;
for(int i = 1; i <= n; i++)
{
U[{rand(), rand()}] = i;
}
cout << "| " << (double)(clock() - t0) / 1000 << "ms ";
sum = 0;
t0 = clock();
for(int i = 1; i <= m; i++)
{
sum += U[{rand(), rand()}];
}
cout << "| " << (double)(clock() - t0) / 1000 << "ms |" << endl;
}
return 0;
}
三元组测试
| 插入范围 | 查询范围 | map插入时间 | map查询时间 | unordered_map插入时间 | unordered_map查询时间 |
|---|---|---|---|---|---|
| 10000 | 10000 | 9.265ms | 10.061ms | 14.415ms | 4.325ms |
| 100000 | 100000 | 127.82ms | 141.59ms | 196.931ms | 70.29ms |
| 1000000 | 1000000 | 1700.73ms | 1971.21ms | 2685.96ms | 782.957ms |
O2
单 int测试
| 插入范围 | 查询范围 | map插入时间 | map查询时间 | unordered_map插入时间 | unordered_map查询时间 |
|---|---|---|---|---|---|
| 10000 | 10000 | 2.103ms | 2.617ms | 3.775ms | 1.261ms |
| 100000 | 100000 | 46.243ms | 67.461ms | 86.591ms | 34.024ms |
| 1000000 | 1000000 | 822.828ms | 1056.85ms | 1412.39ms | 422.122ms |
| 10000000 | 10000000 | 13690.2ms | 16854.1ms | 20994.4ms | 4903.84ms |
二元组测试
| 插入范围 | 查询范围 | map插入时间 | map查询时间 | unordered_map插入时间 | unordered_map查询时间 |
|---|---|---|---|---|---|
| 10000 | 10000 | 2.463ms | 3.461ms | 4.908ms | 2.209ms |
| 100000 | 100000 | 61.531ms | 89.114ms | 127.359ms | 49.821ms |
| 1000000 | 1000000 | 1301.74ms | 1692.79ms | 2184.67ms | 585.508ms |
| 10000000 | 10000000 | 21245.7ms | 24632.5ms | 30906.3ms | 7312.4ms |
理论复杂度
| map<int, int> | unordered_map<int, int> | ||
|---|---|---|---|
| 插入 | \(O(log( n ) )\) | \(O(log(n / m)\) m = 桶数 | |
| 读取 | \(O(log( n ) )\) | \(O(1) ?\) |
可以发现随着数据量的增大unordered_map在插入上由于冲突,性能是不及map的插入\(O(logn)\)的,但是其优秀的\(O(1) ?\)读取具有很大的性能优势
关于c++ STL map 和 unordered_map 的效率的对比测试的更多相关文章
- STL——map/unordered_map基础用法
map /multimap map是STL里重要容器之一. 它的特性总结来讲就是:所有元素都会根据元素的键值key自动排序(也可根据自定义的仿函数进行自定义排序),其中的每个元素都是<key, ...
- STL中的map、unordered_map、hash_map
转自https://blog.csdn.net/liumou111/article/details/49252645 在之前使用STL时,经常混淆的几个数据结构,特别是做Leetcode的题目时,对于 ...
- (转载)STL map与Boost unordered_map的比较
原链接:传送门 今天看到 boost::unordered_map,它与 stl::map的区别就是,stl::map是按照operator<比较判断元素是否相同,以及比较元素的大小,然后选择合 ...
- STL中的map和unordered_map
STL中的map和unordered_map map 头文件:#include 原理:std::map的内部实现了一颗红黑树,有对其键值进行排序的功能,所以map是一个有序的容器,map中的每一个元素 ...
- STL ——map、set、unordered_map、unordered_set
1.map和set map和set底层实现均是红黑树 map支持下标操作,set不支持下标操作. set的迭代器是const的,不允许修改元素的值:map允许修改value,但不允许修改key. se ...
- map 与 unordered_map
两者效率对比: #include <iostream> #include <string> #include <map> #include <unordere ...
- STL MAP及字典树在关键字统计中的性能分析
转载请注明出处:http://blog.csdn.net/mxway/article/details/21321541 在搜索引擎在通常会对关键字出现的次数进行统计,这篇文章分析下使用C++ STL中 ...
- STL map 用法
首先make_pair Pairs C++标准程序库中凡是"必须返回两个值"的函数, 也都会利用pair对象 class pair可以将两个值视为一个单元.容器类别map和mul ...
- STL map详细用法和make_pair函数
今天练习华为上机测试题,遇到了map的用法,看来博客http://blog.csdn.net/sprintfwater/article/details/8765034:感觉很详细,博主的其他内容也值得 ...
随机推荐
- 谁知道百会CRM跟Zoho是一家公司吗?
说到ZohoCRM,无论是搜索引擎还是信息网站,总会有无数的身影.很多人不知道这两家公司的关系,甚至认为百会和Zoho是一家公司.那么,百会CRM和Zoho属于同一类公司吗?它们之间有什么关系?今天小 ...
- Docker安装单点RabbitMQ
环境准备 Centos 7.5 docker环境 安装步骤 拉取镜像 $ docker pull rabbitmq:management 说明: 获取rabbiymq镜像的时候要记得获取managem ...
- SpringCloud:Eureka 配置心跳机制
Server服务端 server: port: 8761 eureka: client: #实例是否在eureka服务器上注册自己的信息以提供其他服务发现,默认为true re ...
- 其他:Spring5.0框架源码导入IDEA
1.下载Spring spring-framework-5.0.4.RELEASE下载地址:https://github.com/spring-projects/spring-framework/re ...
- 在使用XStream时没有processAnnotations方法
https://stackoverflow.com/questions/28770909/xstream-processannotations 我遇到这个问题的原因是xstream.jar的版本问题 ...
- Vector ArrayList LinkedList
三者都实现了List接口! Vector与ArrayList:采用顺序存储的方式,但是Vector是线程安全的,ArrayList是线程不安全的,按需使用: 当存储空间不足的时候,ArrayList默 ...
- Linux学习之路第四天(运行级别)
linux 实用指令 指定运行级别 运行级别说明 0 :关机 1:单用户(找回丢失密码) 2.多用户状态没有网络服务 3.多用户状态有网络服务 4.系统未保留给用户 5.图形界面 6.系统重启 常用的 ...
- 【重学Java】IO流
IO流的UML类图 File类 File类概述和构造方法[应用] File类介绍 它是文件和目录路径名的抽象表示 文件和目录是可以通过File封装成对象的 对于File而言,其封装的并不是一个真正存在 ...
- Nacos配置中心功能
目录 Nacos的配置管理模型 命名空间(Namespace) 配置分组(Group) 配置集(Data ID) 配置项 一个最佳实践 命名空间管理 配置管理 参考 Nacos的配置管理模型 对于Na ...
- FreeRTOS常用函数
一.任务 任务创建和删除xTaskCreate 任务创建xTaskDelete ...