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
原创粉丝点击