1.环境:vs2017编译器 ->Release模式
2.系统内存:8G ,Windows10 专业版
class FirstCPPCls
{
private:
string serviceId;
string systemId;
string subSystemId;
string appVersion;
string companyId;
string clusterName;
public:
FirstCPPCls(void) {}
~FirstCPPCls(void) {}
inline string getServiceId() { return serviceId; }
inline string getSystemId() { return systemId; }
inline string getSubSystemId() { return subSystemId; }
inline string getAppVersion() { return appVersion; }
inline string getCompanyId() { return companyId; }
inline string getClusterName() { return clusterName; }
inline void setServiceId(string v) { serviceId = v; }
inline void setSystemId(string v) { systemId = v; }
inline void setSubSystemId(string v) { subSystemId = v; }
inline void setAppVersion(string v) { appVersion = v; }
inline void setCompanyId(string v) { companyId = v; }
inline void setClusterName(string v) { clusterName = v; }
};
int main(){
#if 1
DWORD begin = GetTickCount();
unordered_map<int, FirstCPPCls*> unordered_map_obj;
for (int i = 0; i < 10000000; i++) {
FirstCPPCls clz;
clz.setAppVersion("12.32.33");
clz.setClusterName("osm-service");
clz.setCompanyId("239383");
clz.setServiceId("sysL.1.223");
clz.setSubSystemId("23");
clz.setSystemId("32");
unordered_map_obj.insert(make_pair(i, &clz));
//printf("\n"); //打印真的很耗性能
}
std::cout << unordered_map_obj.size() << endl;
DWORD end = GetTickCount();
// 打印时间差
cout << "《unordered_map》1000万条数据在栈中存储,消耗的时间:" << (end - begin) << endl; // 平均30豪秒左右
int find_i = 555;
begin = GetTickCount();
unordered_map<int, FirstCPPCls*>::iterator unordered_it;
for (unordered_it = unordered_map_obj.begin(); unordered_it != unordered_map_obj.end(); unordered_it++)
{
if (unordered_it->first == find_i)
{
printf("找到需要元素\n");
}
}
end = GetTickCount();
cout << "《unordered_map》1000万条数据遍历,消耗的时间:" << (end - begin) << endl; // 平均0豪秒左右
begin = GetTickCount();
unordered_it = unordered_map_obj.find(find_i);
if (unordered_it != unordered_map_obj.end())
{
printf("找到需要元素\n");
}
end = GetTickCount();
cout << "《unordered_map》1000万条中查找某个元素,消耗的时间:" << (end - begin) << endl; // 平均0豪秒左右
#endif
return 0;
}
3.测试结果