boost::unordered_map 和 std::map 的对比(包括速度和内存消耗)
2016-01-05 17:30
190 查看
背景:
最近处理的单个文件,大概有13GB,数量条数约5000万。一次性读人到内存需要选择合适的数据结构对其进行存储。本文对比boost::unordered_map 和 std::map
这两种数据结构在该使用情景下的效率。
代码:
![](http://img.blog.csdn.net/20160105172840424?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
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 都更具有优势。
最近处理的单个文件,大概有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 都更具有优势。
相关文章推荐
- iOS UIView动画详解(Objective-C)
- 2016-01-05 FFC
- 静态代理和动态代理的再学习
- SimpleAdapter中使用Drawable和Bitmap对象的方法
- Xcode7以后访问http加密
- 闭包的应用
- java走迷宫
- java调用Explorer.exe打开文件夹选中文件
- Eclipse GlassFish Server 配置
- HLJU——15级寒假培训3 题解
- ViewPager 实现 GallerView 效果,  显示左右两边
- Redis 集群的合纵与连横
- 简单实用的json+struts2+ajax异步校验
- office2010+破解
- QUESTION 66 To recover from the loss of the entire database, including all current control files, al
- 52.Single Number III
- Ehcache详细解读(转)
- Eclipse GlassFish Server 配置
- fedora21安装xmind7
- LeetCode Pascal's Triangle II