【问题标题】:What container type provides better (average) performance than std::map?哪种容器类型提供比 std::map 更好的(平均)性能?
【发布时间】:2010-04-02 20:25:21
【问题描述】:

在下面的示例中,std::map 结构填充了 26 个值,从 A - Z(作为键)和 0 - 26 个作为值。查找最后一个条目(10000000 次)所花费的时间(在我的系统上)对于向量大约是 250 毫秒,对于地图大约是 125 毫秒。 (我使用发布模式编译,为 g++ 4.4 开启了 O3 选项)

但如果出于某种奇怪的原因我想要比 std::map 更好的性能,我需要考虑使用哪些数据结构和函数?

如果答案对您来说显而易见,我深表歉意,但我在 C++ 编程的性能关键方面没有太多经验。

#include <ctime>
#include <map>
#include <vector>
#include <iostream>

struct mystruct
{
    char key;
    int value;

    mystruct(char k = 0, int v = 0) : key(k), value(v) { }
};

int find(const std::vector<mystruct>& ref, char key)
{
    for (std::vector<mystruct>::const_iterator i = ref.begin(); i != ref.end(); ++i)
        if (i->key == key) return i->value;

    return -1;
}

int main()
{
    std::map<char, int> mymap;
    std::vector<mystruct> myvec;

    for (int i = 'a'; i < 'a' + 26; ++i)
    {
        mymap[i] = i - 'a';
        myvec.push_back(mystruct(i, i - 'a'));
    }

    int pre = clock();

    for (int i = 0; i < 10000000; ++i)
    {
        find(myvec, 'z');
    }

    std::cout << "linear scan: milli " << clock() - pre << "\n";

    pre = clock();

    for (int i = 0; i < 10000000; ++i)
    {
        mymap['z'];
    }

    std::cout << "map scan: milli " << clock() - pre << "\n";

    return 0;
}

【问题讨论】:

    标签: c++ map performance


    【解决方案1】:

    对于您的示例,请使用 int value(char x) { return x - 'a'; }

    更概括地说,由于“键”是连续且密集的,因此使用数组(或向量)来保证 Θ(1) 访问时间。

    如果您不需要对键进行排序,use unordered_map,它应该为大多数操作提供摊销对数改进(即 O(log n) -> O(1))。

    (有时,特别是对于小数据集,线性搜索比哈希表(unordered_map)/平衡二叉树(map)更快,因为前者的算法要简单得多,从而减少了big-O中的隐藏常数。简介,个人资料,个人资料。)

    【讨论】:

    • 好答案,我重申。无序映射,然后优化调用(删除面向对象的代码也有帮助......但我可能会因为这样说而被激怒:-)
    • @Etamar:我不认为删除 OO 代码会有帮助,除非你经常使用 virtual
    • @Etamar:你的意思是摆脱课程吗?这将如何加快速度?
    【解决方案2】:

    对于初学者,如果您想比较搜索时间,您可能应该使用std::map::findoperator[] 具有常规查找之外的附加功能。

    此外,您的数据集非常小,这意味着整个向量很容易放入处理器缓存中;许多现代处理器都针对这种蛮力搜索进行了优化,因此您最终会获得相当不错的性能。该映射虽然在理论上具有更好的性能(O(log n) 而不是 O(n)),但并不能真正利用其较少比较次数的优势,因为没有那么多键可供比较,而且它的开销数据布局对它不利。

    对于这么小的数据结构的 TBH,不使用向量带来的额外性能增益通常可以忽略不计。当您处理大量数据和要搜索的分布良好的数据集时,std::map 等“更智能”的数据结构就会发挥作用。

    【讨论】:

      【解决方案3】:

      如果你真的只有从 A 到 Z 的所有条目的值,为什么不使用字母(适当调整)作为向量的索引?:

      std::vector<int> direct_map;
      direct_map.resize(26);
      
      for (int i = 'a'; i < 'a' + 26; ++i) 
      {
          direct_map[i - 'a']= i - 'a';
      }
      
      // ...
      
      int find(const std::vector<int> &direct_map, char key)
      {
          int index= key - 'a';
          if (index>=0 && index<direct_map.size())
              return direct_map[index];
      
          return -1;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-01-11
        • 1970-01-01
        • 1970-01-01
        • 2021-05-02
        • 2023-03-21
        • 2014-06-08
        • 2011-04-27
        相关资源
        最近更新 更多