简体   繁体   English

VB.NET中Dictionary(Of String,SomeReferenceType)的性能

[英]Performance of Dictionary(Of String, SomeReferenceType) in VB.NET

How does performance for reading/adding values from/to Dictionary(Of String, SomeReferenceType) depend on the number of records already entered? 从/到Dictionary(Of String,SomeReferenceType)读取/添加值的性能如何取决于已输入的记录数? I mean, does time increase as O(1), O(log n), O(n) when n goes to be large, or in some other way? 我的意思是,当n变大时,O(1),O(log n),O(n)或其他方式的时间会增加吗?

Dim index As New Dictionary(Of String, SomeReferenceType)

' N entries added in a loop
' ...

Dim id As Integer = 123456789 ' 9-digit number
Dim key As String = id.ToString()
Dim value As New SomeReferenceType

index(key) = value ' Need to estimate this
value = index.TryGetValue(key) ' and this operations depending on N (for large N)

Also, what happens if there is lack of memory? 此外,如果缺乏记忆会发生什么? Should we set capacity of dictionary before entering elements to avoid copying it in case there is not enough memory space? 我们应该在输入元素之前设置字典的容量,以避免在没有足够的内存空间的情况下复制它吗? How long does this operation (copy dictionary to a new place if needed) take depending on N? 这个操作需要多长时间(如果需要,将字典复制到新的地方)取决于N?

The performance for getting an item from a Dictionary is affected very little by the number of items. Dictionary获取项目的性能受项目数量的影响很小。 The items are divided into buckets accoding to their hash code, so there are generally only a single or very few items in each bucket. 这些项目根据其哈希码分为多个桶,因此每个桶中通常只有一个或几个项目。 The operation is close to a O(1) operation. 该操作接近O(1)操作。

Adding items is also close to an O(1) operation. 添加项目也接近O(1)操作。 If the capacity has to be increased you will get a performance hit, but by average that is very small. 如果必须增加容量,您将获得性能损失,但平均值非常小。 As the capacity doubles each time, the amount of data that is moved when increasing the capacity is really not that much. 由于每次容量增加一倍,增加容量时移动的数据量实际上并不多。 The data is by average moved 1.3 times extra, so the average extra work for each add comes down to moving about 16 bytes. 数据平均移动了1.3倍,因此每次添加的平均额外工作量下降到大约16个字节。

If you know how large the Dictionary will get, or just have a decent estimate, you should use that when you create it, to reduce or elliminate the need to increase the capacity. 如果你知道Dictionary会有多大,或者只是有一个不错的估计,你应该在创建它时使用它,以减少或消除增加容量的需要。

Dictionary is implemented with Hastables - Thus O(1). Dictionary是用Hastables实现的 - 因此O(1)。

SortedDictionary is implemented with binary trees (red/black) - Thus O(log n) SortedDictionary使用二叉树(红色/黑色)实现 - 因此O(log n)

SortedList is implemented with lists+binary search - Thus O(n) SortedList使用列表+二进制搜索实现 - 因此O(n)

All dictionaries increase their size automatically (performance is O(n) here but this doesn't happen often because the size is precomputed intelligently, so the approximate performance is O(1) anyway - See this ) 所有字典都会自动增加它们的大小(性能在这里是O(n)但这种情况不会经常发生,因为大小是智能预先计算的,所以无论如何大致的性能是O(1) - 见这个

Just google them and look in the microsoft documentation. 只需谷歌他们,并查看微软文档。

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

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