为什么我的机器上的hash_map和unordered_map非常慢? [英] Why hash_map and unordered_map on my machine are extremely slow?

查看:123
本文介绍了为什么我的机器上的hash_map和unordered_map非常慢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用这段代码测试了这些代码(在Visual Studio 2010 sp1上):

I tested them with this code (on 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
按任意键继续。 。 。

In DEBUG: map: 0.289 unordered_map: 10.738 hash_map: 10.58 Press any key to continue . . .

在RELEASE中:
地图:0.101
unordered_map:0.463
hash_map:0.429
按任意键继续。 。

In RELEASE: map: 0.101 unordered_map: 0.463 hash_map: 0.429 Press any key to continue . . .

推荐答案


  1. 您只在每张地图中插入65536个项目 - 不够大O(log N)和O(1)之间的差异意味着很多。

  2. 您只是插入项目,之后不进行任何搜索。 / li>
  3. 您的密钥全部是连续的整数,不适合通常使用任何地图。

  1. You're only inserting 65536 items in each map -- not large enough for the difference between O(log N) and O(1) to mean a whole lot.
  2. You're only inserting items, not doing any searching afterwards.
  3. Your keys are all contiguous integers in increasing order -- doesn't fit how any map will typically be used.

底线:这不太可能告诉你有关数据结构的问题。

Bottom line: this isn't likely to tell you much about the data structures in question.

这篇关于为什么我的机器上的hash_map和unordered_map非常慢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆