简体   繁体   English

几个元素的map vs unordered_map

[英]map vs unordered_map for few elements

I am trying to choose between map and unordered_map for the following use case: 我试图在mapunordered_map之间选择以下用例:

The key of the map is a pointer. map的关键是指针。 The most common use case is that there will be a single element in the map. 最常见的用例是地图中将有一个元素。 In general, the max number of elements in the map less than 10. The map is accessed very often and speed is the most important factor. 通常,地图中的最大元素数小于10.地图经常被访问,速度是最重要的因素。 Changes to the map are infrequent. 对地图的更改很少发生。

While measuring the speed is obviously the correct approach here, this code will be used on several platforms so I'm trying to create a general rule of thumb for choosing between a map and unordered_map based on number of elements. 虽然测量速度显然是正确的方法,但这个代码将在几个平台上使用,所以我试图创建一个通用的经验法则,根据元素的数量在mapunordered_map之间进行选择。 I've seen some posts here that hint that std::map may be faster for a small number elements, but no definition of "small" was given. 我在这里看到一些帖子暗示std :: map对于少数元素可能更快,但没有给出“小”的定义。

Is there a rule of thumb for when to choose between a map and unordered_map based on number of elements? 是否有基于元素数量在何时根据mapunordered_map进行选择的经验法则? Is another data structure (such as linear search through a vector ) even better? 另一种数据结构(例如通过vector线性搜索)是否更好?

Under the premise that you always need to measure in order to figure out what's more appropriate in terms of performance, if all these things are true: 在你总是需要测量以便找出在性能方面更合适的前提下,如果所有这些都是真的:

  1. Changes to the map are not frequent; 地图的变化不常见;
  2. The map contains a maximum of 10 elements; 地图最多包含10个元素;
  3. Lookups will be frequent; 查找会很频繁;
  4. You care a lot about performance; 你非常关心表现;

Then I would say you would be better off putting your elements in an std::vector and performing a plain iteration over all your elements to find the one you're looking for. 然后我会说你最好把你的元素放在一个std::vector然后对你所有的元素进行一次简单的迭代,找到你正在寻找的元素。

An std::vector will allocate its elements in a contiguous region of memory, so cache locality is likely to grant you a greater performance - the time required to fetch a cache line from main memory after a cache miss is at least one order of magnitude higher than the time required to access the CPU cache. std::vector将在连续的内存区域中分配其元素,因此缓存局部性可能会为您提供更高的性能 - 在缓存未命中之后从主内存获取缓存行所需的时间至少为一个数量级高于访问CPU缓存所需的时间。

Quite interestingly, it seems like Boost's flat_map is ideal for your use case (courtesy of Praetorian ): 非常有趣的是,看起来Boost的flat_map非常适合您的用例(由Praetorian提供 ):

flat_map is similar to std::map but it's implemented like an ordered vector. flat_map类似于std::map但它实现为有序向量。 ( from the online documentation ) 来自在线文档

So if using Boost is an option for you, you may want to try this one. 因此,如果您使用Boost是一个选项,您可能想尝试这个。

I believe for your case of 10 elements or less and usually only one a linear search of an unsorted vector will work best. 我相信你的10个元素或更少的元素,通常只有一个线性搜索未分类的矢量将是最好的。 However, depending on the hash algorithm used the unordered_map may be faster instead. 但是,根据使用的散列算法,unordered_map可能会更快。

It should be easy enough for you to benchmark. 你应该很容易进行基准测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM