C# .NET HashTable 与字典 - 字典可以一样快吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1089132/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 07:58:00  来源:igfitidea点击:

.NET HashTable Vs Dictionary - Can the Dictionary be as fast?

c#.netcollectionsdictionaryhashtable

提问by Jon

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.

我试图弄清楚何时以及为什么使用字典或哈希表。我在这里进行了一些搜索,发现人们在谈论我完全同意的 Dictionary 的通用优势,这导致装箱和拆箱优势略有提高。

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.

但我也读过字典不会总是按照插入的顺序返回对象,它是排序的。作为 HashTable 的地方。据我了解,这会导致 HashTable 在某些情况下要快得多。

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).

我的问题是,这些情况可能是什么?我上面的假设错了吗?您可能会在哪些情况下选择一种高于另一种的情况(是的,最后一种有点模棱两可)。

采纳答案by Mehrdad Afshari

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

System.Collections.Generic.Dictionary<TKey, TValue>System.Collections.Hashtable类都在内部维护一个哈希表数据结构。它们都不能保证保留项目的顺序。

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

撇开装箱/拆箱问题不谈,大多数情况下,它们应该具有非常相似的性能。

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

它们之间的主要结构差异是Dictionary依靠链接(为每个哈希表桶维护一个项目列表)来解决冲突,而Hashtable使用重新散列来解决冲突(当发生冲突时,尝试另一个哈希函数将键映射到桶) .

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

Hashtable如果您的目标是 .NET Framework 2.0+,那么使用类几乎没有什么好处。它被Dictionary<TKey, TValue>.

回答by Adam Luter

Both are effectively the same class (you can look at the disassembly). HashTable was created first before .Net had generics. Dictionary, however is a generic class and gives you strong typing benefits. I would never use HashTable since Dictionary costs you nothing to use.

两者实际上是同一个类(您可以查看反汇编)。HashTable 是在 .Net 有泛型之前首先创建的。但是,Dictionary 是一个泛型类,可为您提供强大的打字优势。我永远不会使用 HashTable,因为 Dictionary 不需要您使用任何费用。

回答by Steven

Another important difference is that the Hashtable type supports lock-free multiple readers and a single writer at the same time, while Dictionary does not.

另一个重要的区别是 Hashtable 类型支持同时无锁的多个读取器和单个写入器,而 Dictionary 不支持。

回答by Abdul Munim

I guess it doesn't mean anything to you now. But just for reference for people stopping by

我想现在对你来说没有任何意义。但仅供路过的人参考

Performance Test - SortedList vs. SortedDictionary vs. Dictionary vs. Hashtable

性能测试 - SortedList vs. SortedDictionary vs. Dictionary vs. Hashtable

Memory allocation:

内存分配:

Memory usage performance test

内存使用性能测试

Time used for inserting:

插入时间:

Time used for inserting

插入时间

Time for searching an item:

搜索项目的时间:

Time for searching an item

搜索项目的时间

回答by jitendra mahaapatro

Dictionary is faster than hashtable as dictionary is a generic strong type. Hashtable is slower as it takes object as data type which leads to boxing and unboxing.

字典比哈希表快,因为字典是通用的强类型。Hashtable 较慢,因为它将对象作为数据类型,这会导致装箱和拆箱。

回答by Juan Camilo Caro J.

MSDN Article: "The Dictionary<TKey, TValue>class has the same functionality as the Hashtableclass. A Dictionary<TKey, TValue>of a specific type (other than Object) has better performance than a Hashtablefor value types because the elements of Hashtableare of type Objectand, therefore, boxing and unboxing typically occur if storing or retrieving a value type".

MSDN 文章:“Dictionary<TKey, TValue>该类具有与类相同的功能HashtableDictionary<TKey, TValue>特定类型的A (除了ObjectHashtable比值类型的A具有更好的性能 ,因为 的元素Hashtable属于类型Object,因此,如果存储或检索值类型”。

Link: http://msdn.microsoft.com/en-us/library/4yh14awz(v=vs.90).aspx

链接:http: //msdn.microsoft.com/en-us/library/4yh14awz(v=vs.90).aspx

回答by ToXinE

If you care about reading that will always return the objects in the order they are inserted in a Dictionary, you may have a look at

如果您关心阅读总是按照它们在字典中插入的顺序返回对象,您可以看看

OrderedDictionary- values can be accessed via an integer index (by order in which items were added) SortedDictionary- items are automatically sorted

OrderedDictionary- 可以通过整数索引访问值(按添加项目的顺序) SortedDictionary- 项目自动排序

回答by user2771704

Differences between Hashtable and Dictionary

哈希表和字典的区别

Dictionary:

字典:

  • Dictionary returns error if we try to find a key which does not exist.
  • Dictionary faster than a Hashtable because there is no boxing and unboxing.
  • Dictionary is a generic type which means we can use it with any data type.
  • 如果我们尝试查找不存在的键,字典会返回错误。
  • 字典比哈希表更快,因为没有装箱和拆箱。
  • Dictionary 是一种泛型类型,这意味着我们可以将它用于任何数据类型。

Hashtable:

哈希表:

  • Hashtable returns null if we try to find a key which does not exist.
  • Hashtable slower than dictionary because it requires boxing and unboxing.
  • Hashtable is not a generic type,
  • 如果我们试图找到一个不存在的键,Hashtable 将返回 null。
  • 哈希表比字典慢,因为它需要装箱和拆箱。
  • Hashtable 不是泛型类型,

回答by NullReference

Another important difference is that Hashtableis thread safe. Hashtablehas built in multiple reader/single writer (MR/SW) thread safety which means Hashtableallows ONE writer together with multiple readers without locking. In the case of Dictionarythere is no thread safety, if you need thread safety you must implement your own synchronization.

另一个重要的区别Hashtable是线程安全。Hashtable内置Hashtable了多读/单写 (MR/SW) 线程安全,这意味着允许一个写与多个读者一起使用而无需锁定。在Dictionary没有线程安全的情况下,如果需要线程安全就必须实现自己的同步。

To elaborate further:

进一步阐述:

Hashtable, provide some thread-safety through the Synchronized property, which returns a thread-safe wrapper around the collection. The wrapper works by locking the entire collection on every add or remove operation. Therefore, each thread that is attempting to access the collection must wait for its turn to take the one lock. This is not scalable and can cause significant performance degradation for large collections. Also, the design is not completely protected from race conditions.

The .NET Framework 2.0 collection classes like List<T>, Dictionary<TKey, TValue>, etc do not provide any thread synchronization; user code must provide all synchronization when items are added or removed on multiple threads concurrently If you need type safety as well thread safety, use concurrent collections classes in the .NET Framework. Further reading here.

Hashtable,通过 Synchronized 属性提供一些线程安全性,该属性返回一个围绕集合的线程安全包装器。包装器通过在每次添加或删除操作时锁定整个集合来工作。因此,试图访问集合的每个线程都必须等待轮到它来获取一个锁。这是不可扩展的,并且可能导致大型集合的性能显着下降。此外,该设计并未完全免受竞争条件的影响。

.NET Framework 2.0中的集合类一样 List<T>Dictionary<TKey, TValue>等不提供任何线程同步; 当在多个线程上同时添加或删除项目时,用户代码必须提供所有同步 如果您需要类型安全以及线程安全,请使用 .NET Framework 中的并发集合类。进一步阅读这里。

回答by janeon

Dictionaries have the advantage of being a generic type, which makes it type safe and a bit faster due to lack of need for boxing. The following comparison table(constructed using the answers found in a similar SO question post) illustrates some of the other reasons that support dictionaries over hash tables (or vice versa).

字典的优点是作为泛型类型,这使得它类型安全并且由于不需要装箱而速度更快。下面的比较表(使用在类似的 SO问题帖子中找到的答案构建)说明了支持字典而不是哈希表的一些其他原因(反之亦然)。