您的位置:首页 > 其它

boost::unordered_map 和 std::map 的对比(包括速度和内存消耗)

2016-01-05 17:30 190 查看
背景:

最近处理的单个文件,大概有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 都更具有优势。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: