boost::unordered_map 和 std::map 的对比(包括速度和内存消耗)
来源:互联网 发布:公司网络无法玩游戏 编辑:程序博客网 时间:2024/06/05 11:41
背景:
最近处理的单个文件,大概有13GB,数量条数约5000万。一次性读人到内存需要选择合适的数据结构对其进行存储。本文对比boost::unordered_map 和 std::map
这两种数据结构在该使用情景下的效率。
代码:
#include "boost/unordered_map.hpp"#include <iostream>#include <map>#include "time.h"using namespace std;int main(){{time_t first_time = time(0);boost::unordered_map<int, int> test_hash;for (int i = 0; i < 50000000; i++){test_hash.insert(std::pair<int, int>(i, i));}cout << test_hash.size() << endl;time_t second_time = time(0);cout << "The insert operate cost: " << second_time - first_time << endl;for (int i = 0; i< 50000001; ++i){boost::unordered_map<int, int>::iterator iter = test_hash.find(i);if (iter == test_hash.end()){cout << "unordered map find the end!" << endl;}}time_t third_time = time(0);cout << "The find operate cost: " << third_time - second_time << endl;}{time_t first_time = time(0);std::map<int, int> test_hash;for (int i = 0; i < 50000000; i++){test_hash.insert(std::pair<int, int>(i, i));}cout << test_hash.size() << endl;time_t second_time = time(0);cout << "The insert operate cost: " << second_time - first_time << endl;for (int i = 0; i< 50000001; ++i){std::map<int, int>::iterator iter = test_hash.find(i);if (iter == test_hash.end()){cout << "map find the end!" << endl;}}time_t third_time = time(0);cout << "The find operate cost: " << third_time - second_time << endl;}return 0;}运行结果如下所示:
50000000
The insert operate cost: 34
unordered map find the end!
The find operate cost: 15
50000000
The insert operate cost: 49
map find the end!
The find operate cost: 23
效率上:
boost::unordered_map (34s)插入比map(49s)快。
boost::unordered_map (15s)查找操作比map(23s)快。
内存空间占用上:
boost::unordered_map 内存占用26%。7.6GB*0.26=1.976GB。
map内存占用29%。7.6GB*0.29=2.2GB。
所以,在效率和内存占用上面,boost::unordered_map 都更具有优势。
0 0
- boost::unordered_map 和 std::map 的对比(包括速度和内存消耗)
- boost:unordered_map和std::map的使用详解和性能比较
- 使用std::map和std::list存放数据,消耗内存比实际数据大得多
- map、set和unordered_map、unordered_set对比
- unordered_map和map的区别
- C++11:基于std::unordered_map和共享锁构建线程安全的map
- STL:map和BOOST:unordered_map 实现简单比较
- unordered_map 与 map 的对比
- map、hash_map和unordered_map
- unordered_map和map
- unordered_map和map
- LeetCode 1 Two Sum(unordered_map和map的区别)
- STL map和unordered_map的区别
- unordered_map 与 map 的区别和联系
- 关于Matlab的速度和内存消耗的转载和自己的感受
- STL map与Boost unordered_map 的不同
- 字符串拼接执行速度和内存消耗比较
- std::unordered_map(提供自己的Hash函数和等价准则)
- 欢迎使用CSDN-markdown编辑器
- c printf
- Java 8 正式发布,新特性全搜罗
- 安卓-无序广播
- linux平台常用压缩和解压缩命令
- boost::unordered_map 和 std::map 的对比(包括速度和内存消耗)
- 简单配置nginx使之支持pathinfo
- Android中AppWidget的分析与应用:AppWidgetProvider
- iOS 长按手势执行两次解决办法
- python的运算符重载
- 学习前言
- iPlanet - SSL安装流程
- OpenCV中遍历图像的基本方式
- android 虚拟按键遮挡布局问题