std::map std::unordered_map 性能测试

本文通过性能测试展示了在不同环境下,std::map与std::unordered_map在插入、删除和查找操作上的表现。在win7 VS2013 Debug和Release版本,以及CentOS 7.0 GCC 4.8.5中,std::unordered_map的查找速度大约是std::map的两倍,但在插入和删除方面,std::map可能更优。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

先上图:win7 vs2013 debug版本:

win7 vs2013 release版本:

centos7.0 gcc4.8.5 -g  (我是在虚拟机里,内存比较少,所以只分配了两万个):

从测试结果可以看出:std::unordered_map插入、删除的性能会比std::map稍差一点,但是查找速度std::unordered_map和std::map的比例基本上是2:1

测试代码:

map_test.h:

#include <map>
#include <iostream>
#include <chrono>
#include <string>
#include <stdlib.h>
#include <stdio.h>

using namespace std;

extern chrono::system_clock::time_point last_time;

#define PRINT_TIME(str) do { auto curr_time = std::chrono::high_resolution_clock::now();\
	std::cout << str << " : " << std::chrono::duration_cast<std::chrono::milliseconds>(curr_time - last_time).count() << endl;\
	last_time = curr_time; } while (false);

int g_nTestTmp = 0;

template<class the_map, class Obj>
void map_test(the_map mapObjects[], const unsigned nTestObjectCount, const unsigned nTestMapCount, Obj& obj)
{

	PRINT_TIME("construct");

	for (unsigned idx = 0; idx < nTestObjectCount; ++idx)
	{
		obj.ID = idx;
		memset(obj.szTest, 0, sizeof(obj.szTest));

		for (size_t mapid = 0; mapid < nTestMapCount; ++mapid)
		{
			mapObjects[mapid][idx] = obj;
		}
	}
	PRINT_TIME("add cost");

	for (unsigned idx = 0; idx < nTestObjectCount; ++idx)
	{
		obj.ID = idx;
		memset(obj.szTest, 0, sizeof(obj.szTest));

		for (size_t mapid = 0; mapid < nTestMapCount; ++mapid)
		{
			typename the_map::iterator it = mapObjects[mapid].find(idx);
			if (it->second.ID == g_nTestTmp)
			{
				g_nTestTmp = 1;
			}
		}
	}
	PRINT_TIME("find cost");

	for (unsigned idx = 0; idx < nTestObjectCount; ++idx)
	{
		for (size_t mapid = 0; mapid < nTestMapCount; ++mapid)
		{
			mapObjects[mapid].erase(idx);
		}
	}
	PRINT_TIME("erase cost");


	for (unsigned idx = 0; idx < nTestObjectCount; ++idx)
	{
		obj.ID = idx;
		memset(obj.szTest, 0, sizeof(obj.szTest));

		for (size_t mapid = 0; mapid < nTestMapCount; ++mapid)
		{
			mapObjects[mapid][idx] = obj;
		}
	}
	PRINT_TIME("add cost");

	for (unsigned idx = 0; idx < nTestObjectCount; ++idx)
	{
		obj.ID = idx;
		memset(obj.szTest, 0, sizeof(obj.szTest));

		for (size_t mapid = 0; mapid < nTestMapCount; ++mapid)
		{
			typename the_map::iterator it = mapObjects[mapid].find(idx);
			if (it->second.ID == g_nTestTmp)
			{
				g_nTestTmp = 1;
			}
		}
	}
	PRINT_TIME("find cost");

	for (unsigned idx = 0; idx < nTestObjectCount; ++idx)
	{
		for (size_t mapid = 0; mapid < nTestMapCount; ++mapid)
		{
			mapObjects[mapid].erase(idx);
		}
	}
	PRINT_TIME("erase cost");


	for (unsigned idx = 0; idx < nTestObjectCount; ++idx)
	{
		obj.ID = idx;
		memset(obj.szTest, 0, sizeof(obj.szTest));

		for (size_t mapid = 0; mapid < nTestMapCount; ++mapid)
		{
			mapObjects[mapid][idx] = obj;
		}
	}
	PRINT_TIME("add cost");

	for (unsigned idx = 0; idx < nTestObjectCount; ++idx)
	{
		obj.ID = idx;
		memset(obj.szTest, 0, sizeof(obj.szTest));

		for (size_t mapid = 0; mapid < nTestMapCount; ++mapid)
		{
			typename the_map::iterator it = mapObjects[mapid].find(idx);
			if (it->second.ID == g_nTestTmp)
			{
				g_nTestTmp = 1;
			}
		}
	}
	PRINT_TIME("find cost");

	for (unsigned idx = 0; idx < nTestObjectCount; ++idx)
	{
		for (size_t mapid = 0; mapid < nTestMapCount; ++mapid)
		{
			mapObjects[mapid].erase(idx);
		}
	}
	PRINT_TIME("erase cost");
}
main.cpp:

#include <map>
#include <unordered_map>
#include <iostream>
#include <chrono>
#include <string>

#include "map_test.h"

using namespace std;

class PoolObj
{
public:
	unsigned ID;
	char szTest[8824];
};

chrono::system_clock::time_point last_time;

int main()
{
#define TEST_OBJECT_COUNT 60000
#define TEST_MAP_COUNT 10

	//std::chrono::time_point<std::chrono::high_resolution_clock> t1 = std::chrono::high_resolution_clock::now();
	last_time = std::chrono::high_resolution_clock::now();

	PoolObj obj;
	cout << "std::map begin TEST_OBJECT_COUNT:" << TEST_OBJECT_COUNT << " TEST_MAP_COUNT:" << TEST_MAP_COUNT << endl;
	{
		map<unsigned, PoolObj> mapObjects[TEST_MAP_COUNT];

		map_test(mapObjects, TEST_OBJECT_COUNT, TEST_MAP_COUNT, obj);
	}
	cout << "std::map end =================================" << endl << endl << endl;


	cout << "std::unordered_map begin TEST_OBJECT_COUNT:" << TEST_OBJECT_COUNT << " TEST_MAP_COUNT:" << TEST_MAP_COUNT << endl;
	{
		unordered_map<unsigned, PoolObj> mapObjects[TEST_MAP_COUNT];
		map_test(mapObjects, TEST_OBJECT_COUNT, TEST_MAP_COUNT, obj);
	}
	cout << "std::unordered_map end =================================" << endl;

	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值