http://www.cnblogs.com/zhjh256/p/6346501.html講述了基本的map操作,在測試的時候,發現map的性能極為低下,與java相比相差了接近200倍。測試的邏輯如下:
// map定義 map<int, FirstCPPCls*> mapStudent; for (i=0;i<10000;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"); mapStudent.insert(pair<int, FirstCPPCls*>(i, &clz)); } // 獲取時間相對計數器, vc專用 begin = GetTickCount(); for (j=0;j<100;j++) { for (f=0;f<10000;f++) { // map查找 mapStudent.find(f); } } end = GetTickCount(); // 打印時間差 cout << "Map查找耗時:" << (end - begin) << endl; // 平均4秒左右 system("pause");
在java中相同的實現,get 100 0000次只花費了20ms。於是搜索 c++ map性能,看了兩個帖子如下:
http://blog.csdn.net/a418382926/article/details/22302907
http://blog.sina.com.cn/s/blog_5f93da790101hxxi.html
http://www.ideawu.net/blog/archives/751.html
隨后,進行hash_map和unordered_map測試,如下:
// hash_map定義 hash_map<int, FirstCPPCls*> hash_mapStudent; for (i=0;i<10000;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"); hash_mapStudent.insert(pair<int, FirstCPPCls*>(i, &clz)); } // 獲取時間相對計數器, vc專用 begin = GetTickCount(); for (j=0;j<100;j++) { for (f=0;f<10000;f++) { // map查找 hash_mapStudent.find(f); } } end = GetTickCount(); // 打印時間差 cout << "HashMap查找耗時:" << (end - begin) << endl; // 平均4秒左右 system("pause"); // hash_map定義 unordered_map<int, FirstCPPCls*> unordered_mapStudent; for (i=0;i<10000;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_mapStudent.insert(pair<int, FirstCPPCls*>(i, &clz)); } // 獲取時間相對計數器, vc專用 begin = GetTickCount(); for (j=0;j<100;j++) { for (f=0;f<10000;f++) { // map查找 unordered_mapStudent.find(f); } } end = GetTickCount(); // 打印時間差 cout << "UnorderedMap查找耗時:" << (end - begin) << endl; // 平均4秒左右 system("pause");
輸出如下:
HashMap查找耗時:1610 請按任意鍵繼續. . . UnorderedMap查找耗時:1797 請按任意鍵繼續. . .
雖然,相比std::map,確實提升了50%多,但是跟java,還是慢的一塌糊塗,因為對stl還沒有研究,不確定具體什么原因導致。