.NET的HashTable VS词典 - 可字典一样快?字典、词典、NET、HashTable

2023-09-02 01:20:52 作者:素言

我试图找出何时以及为什么使用词典或哈希表。我已经做了一下这里的搜索,并发现人们谈论的字典,我有完全同意,这导致了装箱和拆箱的优势有轻微的性能增益的一般优点。

I am trying to figure out when and why to use a Dictionary or a HashTable. I have done a bit of a search on here and have found people talking about the generic advantages of the Dictionary which I totally agree with, which leads the boxing and unboxing advantage for a slight performance gain.

不过,我也看到了词典不会总是返回的对象,他们被插入的顺序,将垃圾分类的事情。凡作为一个哈希表会。据我了解,这导致了哈希表是远远快于某些情况下。

But I have also read the Dictionary will not always return the objects in the order they are inserted, thing it is sorted. Where as a HashTable will. As I understand it this leads to the HashTable being far faster for some situations.

我的问题是真的,什么可能的情况是什么?我只是错在我的假设之上?可能你使用什么样的情况下,选择一个以上的其他,(是最后一个是有点含糊不清)。

My question is really, what might those situations be? Am I just wrong in my assumptions above? What situations might you use to choose one above the other, (yes the last one is a bit ambiguous).

推荐答案

System.Collections.Generic.Dictionary< TKEY的,TValue> 系统.Collections.Hashtable 类在内部保持一个哈希表数据结构。 他们没有保障preserving项目的顺序。

System.Collections.Generic.Dictionary<TKey, TValue> and System.Collections.Hashtable classes both maintain a hash table data structure internally. None of them guarantee preserving the order of items.

离开拳击/拆箱问题外,大部分时间,他们应该有非常相似的性能。

Leaving boxing/unboxing issues aside, most of the time, they should have very similar performance.

它们之间的主要结构差异在于词典依赖的链的(维持对每个散列表桶的项目列表)来解决冲突而的Hashtable 使用的换汤不换药的碰撞分辨率(发生碰撞时,尝试另一种散列函数映射的关键桶)。

The primary structural difference between them is that Dictionary relies on chaining (maintaining a list of items for each hash table bucket) to resolve collisions whereas Hashtable uses rehashing for collision resolution (when a collision occurs, tries another hash function to map the key to a bucket).

有什么好处使用的Hashtable 类,如果你是针对用于.NET Framework 2.0+。它有效地呈现过时词典&LT; TKEY的,TValue&GT;

There is little benefit to use Hashtable class if you are targeting for .NET Framework 2.0+. It's effectively rendered obsolete by Dictionary<TKey, TValue>.