boost:unordered_map和std::map的使用详解和性能比较 - 小众知识

boost:unordered_map和std::map的使用详解和性能比较

2021-08-18 01:04:25 苏内容
  标签: boost
阅读:4141

背景:


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


————————————————

版权声明:本文为CSDN博主「JasonLiu1919」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。

原文链接:https://blog.csdn.net/ljp1919/article/details/50463761


扩展阅读
相关阅读
© CopyRight 2010-2021, PREDREAM.ORG, Inc.All Rights Reserved. 京ICP备13045924号-1