先上图: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;
}