C++ map和hash_map简单对比
来源:互联网 发布:凯佰赫战盾数据 编辑:程序博客网 时间:2024/06/06 12:52
C++ map 和 hash_map 对比
map的基本数据结构是平衡二叉树,hash_map的基础数据结构是hash_table哈希表,下面程序展示了向map和hash_map中插入数据消耗时间对比。
数据量较小的时候可以选择map,数据量大、对插入查找效率要求高的时候选择hash_map。
/*************************************************************************
> File Name: map.cpp
> Author: chenhui
> Mail: *********
> Created Time: 2017年12月21日 17:23:34
************************************************************************/
#include <iostream>
#include <stdio.h>
#include <map>
#include "../Includes/times.h"
#ifdef __GNUC__
#include <ext/hash_map>
#else
#include <hash_map>
#endif
namespace std
{
using namespace __gnu_cxx;
}
using namespace std;
typedef hash_map<int,int> HASHMAPINT2INT;
typedef map<int,int> MAPINT2INT;
int main()
{
MAPINT2INT mapTest;
int64_t timeBegin = getTimeMsec();
for(int i=0;i<5000000;i++)
{
mapTest[i]=i;
}
int64_t timeEnd = getTimeMsec();
printf("map use time:%d\n",timeEnd-timeBegin);
HASHMAPINT2INT hamp;
timeBegin = getTimeMsec();
for(int i=0;i<5000000;i++)
{
hamp[i]=i;
}
timeEnd = getTimeMsec();
printf("hashmap use time:%d\n",timeEnd-timeBegin);
return 0;
map的基本数据结构是平衡二叉树,hash_map的基础数据结构是hash_table哈希表,下面程序展示了向map和hash_map中插入数据消耗时间对比。
数据量较小的时候可以选择map,数据量大、对插入查找效率要求高的时候选择hash_map。
/*************************************************************************
> File Name: map.cpp
> Author: chenhui
> Mail: *********
> Created Time: 2017年12月21日 17:23:34
************************************************************************/
#include <iostream>
#include <stdio.h>
#include <map>
#include "../Includes/times.h"
#ifdef __GNUC__
#include <ext/hash_map>
#else
#include <hash_map>
#endif
namespace std
{
using namespace __gnu_cxx;
}
using namespace std;
typedef hash_map<int,int> HASHMAPINT2INT;
typedef map<int,int> MAPINT2INT;
int main()
{
MAPINT2INT mapTest;
int64_t timeBegin = getTimeMsec();
for(int i=0;i<5000000;i++)
{
mapTest[i]=i;
}
int64_t timeEnd = getTimeMsec();
printf("map use time:%d\n",timeEnd-timeBegin);
HASHMAPINT2INT hamp;
timeBegin = getTimeMsec();
for(int i=0;i<5000000;i++)
{
hamp[i]=i;
}
timeEnd = getTimeMsec();
printf("hashmap use time:%d\n",timeEnd-timeBegin);
return 0;
}
下面是在自己机器上运行的结果:
map use time:4972
hashmap use time:1214
阅读全文
0 0
- C++ map和hash_map简单对比
- STL map 和 hash_map
- hash_map和map
- hash_map和map
- hash_map 和 map
- hash_map和map
- map和hash_map
- map、hash_map和unordered_map
- 关于map、hash_map 和 unordered_map 的简单性能测试
- hash_map和map的区别
- hash_map和map的区别
- map和hash_map的区别
- hash_map和map的区别 .
- hash_map和map的区别
- hash_map和map的区别
- hash_map和map的区别
- hash_map和map的区别
- hash_map和map的区别
- 初步接触jndi
- ILOG学习笔记
- Oracle学习笔记
- MySql含有GROUP BY子句的查询中如何显示COUNT()为0的结果
- 基于css的轮播
- C++ map和hash_map简单对比
- 职场法则
- C语言实现ping功能(Linux & Mac OS系统下)有注释
- Java线程池ExecutorService方法详解
- spring cloud 请求服务失败, 重试配置
- 层次聚类算法原理及实现
- spring boot 使用Swagger2
- 共同学习Java源代码-多线程与并发-AbstractExecutorService类(二)
- Python资料大全