我测试了他们与此代码(在Visual Studio 2010的SP1):
#include <ctime>
#include <iostream>
#include <map>
#include <unordered_map>
#include <hash_map>
int main()
{
clock_t time;
int LOOP = (1 << 16);
std::map<int, int> my_map;
std::unordered_map<int, int> map_unordered_map;
std::hash_map<int, int> my_hash_map;
time = clock();
for (int i = 0; i != LOOP; ++i)
{
my_map[i] = i;
}
std::cout << "map: " << ((double)(clock() - time) / CLOCKS_PER_SEC) << std::endl;
time = clock();
for (int i = 0; i != LOOP; ++i)
{
map_unordered_map[i] = i;
}
std::cout << "unordered_map: " << ((double)(clock() - time) / CLOCKS_PER_SEC) << std::endl;
time = clock();
for (int i = 0; i != LOOP; ++i)
{
my_hash_map[i] = i;
}
std::cout << "hash_map: " << ((double)(clock() - time) / CLOCKS_PER_SEC) << std::endl;
system("PAUSE");
return EXIT_SUCCESS;
}
而结果就是这么奇怪:
在DEBUG:地图:0.289 unordered_map:10.738的hash_map:10.58按任意键继续。 。 。
在发布:地图:0.101 unordered_map:0.463的hash_map:0.429按任意键继续。 。 。