测试代码:

#include <iostream>
using namespace std;
#include <string>
#include <windows.h>
#include <string.h> #include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <map>
const int maxval = 2000000 * 5;
#include <unordered_map> void map_test()
{
printf("map_test\n");
map<int, int> mp;
clock_t startTime, endTime;
startTime = clock();
for (int i = 0; i < maxval; i++)
{
mp[rand() % maxval]++;
}
endTime = clock();
printf("%lf\n", (double)(endTime - startTime) / CLOCKS_PER_SEC);
printf("insert finish\n");
startTime = clock();
for (int i = 0; i < maxval; i++)
{
if (mp.find(rand()%maxval) == mp.end())
{
//printf("not found\n");
}
}
endTime = clock();
printf("%lf\n", (double)(endTime - startTime) / CLOCKS_PER_SEC);
printf("find finish\n"); startTime = clock();
for(auto it = mp.begin(); it!=mp.end(); it++)
{ }
endTime = clock();
printf("%lf\n", (double)(endTime - startTime) / CLOCKS_PER_SEC);
printf("travel finish\n"); printf("------------------------------------------------\n");
} void hash_map_test()
{
printf("hash_map_test\n");
unordered_map<int, int> mp;
clock_t startTime, endTime;
startTime = clock();
for (int i = 0; i < maxval; i++)
{
mp[rand() % maxval] ++;
}
endTime = clock();
printf("%lf\n", (double)(endTime - startTime) / CLOCKS_PER_SEC);
printf("insert finish\n");
startTime = clock();
for (int i = 0; i < maxval; i++)
{
if (mp.find(rand() % maxval) == mp.end())
{
//printf("not found\n");
}
}
endTime = clock();
printf("%lf\n", (double)(endTime - startTime) / CLOCKS_PER_SEC);
printf("find finish\n"); startTime = clock();
for(auto it = mp.begin(); it!=mp.end(); it++)
{ }
endTime = clock();
printf("%lf\n", (double)(endTime - startTime) / CLOCKS_PER_SEC);
printf("travel finish\n"); printf("------------------------------------------------\n");
} int main(int argc, char *argv[])
{
srand(0);
map_test();
Sleep(1000);
srand(0);
hash_map_test(); system("pause");
return 0;
}

详解:

map(使用红黑树)与unordered_map(hash_map)比较

    map理论插入、查询时间复杂度O(logn)

    unordered_map理论插入、查询时间复杂度O(1)


数据量较小时,可能是由于unordered_map(hash_map)初始大小较小,大小频繁到达阈值,多次重建导致插入所用时间稍大。(类似vector的重建过程)。

哈希函数也是有消耗的(应该是常数时间),这时候用于哈希的消耗大于对红黑树查找的消耗(O(logn)),所以unordered_map的查找时间会多余对map的查找时间。


数据量较大时,重建次数减少,用于重建的开销小,unordered_map O(1)的优势开始显现


数据量更大,优势更明显


使用空间:

前半部分为map,后半部分为unordered_map

unordered_map占用的空间比map略多,但可以接受。

map和unordered_map内部实现应该都是采用达到阈值翻倍开辟空间的机制(16、32、64、128、256、512、1024……)浪费一定的空间是不可避免的。并且在开双倍空间时,若不能从当前开辟,会在其他位置开辟,开好后将数据移过去。数据的频繁移动也会消耗一定的时间,在数据量较小时尤为明显。


一种方法是手写定长开散列。这样做在数据量较小时有很好地效果(避免了数据频繁移动,真正趋近O(1))。但由于是定长的,在数据量较大时,数据重叠严重,散列效果急剧下降,时间复杂度趋近O(n)。


一种折中的方法是自己手写unordered_map(hash_map),将初始大小赋为一个较大的值。扩张可以模仿STL的双倍扩张,也可以自己采用其他方法。这样写出来的是最优的,但是实现起来极为麻烦。

综合利弊,我们组采用unordered_map。


附:使用Dev测试与VS2017测试效果相差极大???

效率差了10倍???

原因:

Dev

VS2017

在Debug下,要记录断点等调试信息,的确慢。

Release:不对源代码进行调试,编译时对应用程序的速度进行优化,使得程序在代码大小和运行速度上都是最优的。

VS2017切到release后,还更快

除了前面说的Debug与release导致效率差异外,编译器的不同也会导致效率差异。

学到了。

unordered_map(hash_map)和map的比较的更多相关文章

  1. hash_map和map的区别

    hash_map和map的区别 分类: STL2008-10-15 21:24 5444人阅读 评论(0) 收藏 举报 class数据结构编译器存储平台tree 这里列几个常见问题,应该对你理解和使用 ...

  2. C++中的hash_map和map的区别

    hash_map和map的区别在哪里?构造函数.hash_map需要hash函数,等于函数:map只需要比较函数(小于函数). 存储结构.hash_map采用hash表存储,map一般采用红黑树(RB ...

  3. boost::unordered_map 和 std::map 的效率 与 内存比较

    例子链接:http://blog.csdn.net/gamecreating/article/details/7698719 结论: unordered_map 查找效率快五倍,插入更快,节省一定内存 ...

  4. std::unordered_map与std::map

    前者查找更快.后者自动排序,并可指定排序方式. 资料参考: https://blog.csdn.net/photon222/article/details/102947597

  5. 福大软工1816 · 第五次作业 - 结对作业2_map与unordered map的比较测试

    测试代码: #include <iostream> using namespace std; #include <string> #include <windows.h& ...

  6. STL中的map、unordered_map、hash_map

    转自https://blog.csdn.net/liumou111/article/details/49252645 在之前使用STL时,经常混淆的几个数据结构,特别是做Leetcode的题目时,对于 ...

  7. map、hash_map、unordered_map 的思考

    #include <map> map<string,int> dict; map是基于红黑树实现的,可以快速查找一个元素是否存在,是关系型容器,能够表达两个数据之间的映射关系. ...

  8. map vs hash_map

    1. map, multimap, set, multiset g++ 中 map, multimap, set, multiset 由红黑树实现 map: bits/stl_map.h multim ...

  9. c++ map unordered_map

    map operator<的重载一定要定义成const.因为map内部实现时调用operator<的函数好像是const. #include<string> #include& ...

随机推荐

  1. Julia 1.0 中文文档

    欢迎来到Julia 1.0的文档. 请阅读发布博客文章,了解该语言的一般概述以及自Julia v0.6以来的许多更改.请注意,0.7版本与1.0一起发布,以提供1.0版本之前的软件包和代码的升级路径. ...

  2. java并发(1)

    hashmap效率高单线程不安全,hashTable效率低但线程安全 因为hashTable使用synchronized来保证线程安全,所以效率十分低,比如线程1使用put插入数据时,线程2既不能使用 ...

  3. Applied Cloud Deep Semantic Recognition: Advanced Anomaly Detection(应用云深层语义识别:高级异态检测)

    亚马逊链接 引言 (by Mehdi Roopaei & Paul Rad) 异态检测与情境感知 在数据分析领域,异态检测讲的是在一个数据集中,发现到其中不符合预期模式的物体,动作,行为或事件 ...

  4. vue相关ajax库的使用

    相关库: vue-resource: vue插件, 多用于vue1.x axios: 第三方库, 多用于vue2.x vue-resource使用 // 引入模块 import VueResource ...

  5. 使用MATLAB设计FIR滤波器

    1.      采用fir1函数设计,fir1函数可以设计低通.带通.高通.带阻等多种类型的具有严格线性相位特性的FIR滤波器.语法形式: b = fir1(n, wn) b = fir1(n, wn ...

  6. git配置config记住密码

    设置记住密码(默认15分钟): git config --global credential.helper cache如果想自己设置时间,可以这样做: git config credential.he ...

  7. BZOJ1001_狼抓兔子_KEY

    题目传送门 由题意得是最小割问题,又由最大流最小割定理可得只需要求无向图的最大流即可. 建双向边,跑Dinic,EK会超时. 注意在DFS时要加"if(!res)dist[now]=0;&q ...

  8. 北京Uber优步司机奖励政策(4月3日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  9. 苏州Uber优步司机奖励政策(12月28日到1月3日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  10. LeetCode: 62. Unique Paths(Medium)

    1. 原题链接 https://leetcode.com/problems/unique-paths/description/ 2. 题目要求 给定一个m*n的棋盘,从左上角的格子开始移动,每次只能向 ...