是否有对HybridDictionary的一个通用版本?版本、HybridDictionary

2023-09-04 08:17:14 作者:撕心的想念°

System.Collection.Specialized.HybridDictionary的描述是这样的:

The description of System.Collection.Specialized.HybridDictionary is this:

实现的IDictionary通过使用   System.Collections.Specialized.ListDictionary   而收集小,并且   然后切换到   时的System.Collections.Hashtable的   集合变大。

Implements IDictionary by using a System.Collections.Specialized.ListDictionary while the collection is small, and then switching to a System.Collections.Hashtable when the collection gets large.

有一种等效通用实现?

推荐答案

这并不是说我知道。然而,有一个成熟的需要呢?哈希表没有一个大的开销,即使是非常小的 N 的应该是更快只需使用普通的哈希表,而不是线性搜索

Not that I know of. However, is there a proven need? Hash tables don't have a large overhead, even for very small N it should be faster just to use the normal hash table than to search linearly.

修改我没有一个基准来证明这一点,但只是通过比较我总结出算法,哈希表应该比线性搜索快尽快的 N 的> 6平均(字符串键或类似的非平凡的哈希值),所以我们实在没有理由为一个混合的实现。

EDIT I don't have a benchmark to prove it but just by comparing the algorithms I conclude that hash tables should be faster than linear search as soon as N > 6 on average (for string keys or similar nontrivial hashes) so there is really no reason for a hybrid implementation.

参数如下。在线性搜索,平均一半的元素都被比作你的输入,即 N 的  /  2。在哈希表中,已知的是比较的期望的数量是2,而不管输入的大小(对于小于0.1这实际上是接近1的载荷因子很小哈希表)。此外,哈希必须计算。这导致3操作上的投入,再加上非常小的开销可以忽略不计。因此,我们寻找其中的 N 的这是事实,3>的 N 的  /  2,这是平凡的 N 的 >&NBSP ; 6。

The argument is as follows. In linear search, on average half the elements have to be compared to your input, i.e. N / 2. In a hash table, it is known that the expected number of comparisons is 2, regardless of input size (for very small hash tables with a load factor of less than 0.1 this is actually approaching 1). Additionally, the hash has to be calculated. This results in 3 operations on your input, plus a very small overhead that can be neglected. Thus, we search for which N it is true that 3 > N / 2, which is trivially N > 6.

请注意,上面的计算实际上是错误的,因为这少量的元素,.NET的词典的负荷率会比0.1少得多。因此,引爆点实际上还要低。

Note that the above calculation is actually wrong because for this small number of elements, the load factor of .NET's Dictionary will be much less than 0.1. The tipping point is therefore actually even lower.