SortedList<>、SortedDictionary<>和字典<>
我发现 SortedList
SortedDictionary
和 Dictionary
实现相同的接口。
- 我们什么时候应该选择
SortedList
和SortedDictionary
而不是Dictionary
? -
SortedList
和SortedDictionary
在应用方面有什么区别?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
当迭代两者中的任何一个中的元素时,元素将被排序。
Dictionary
则不然。MSDN 解决了
SortedList
和SortedDictionary
:When iterating over the elements in either of the two, the elements will be sorted. Not so with
Dictionary<T,V>
.MSDN addresses the difference between
SortedList<T,V>
andSortedDictionary<T,V>
:我会提到字典之间的差异。
上图显示
Dictionary
在每种情况下都等于或快于Sorted
模拟,但如果需要元素的顺序(例如打印它们),已排序
选择其中之一。源: http://people.cs .aau.dk/~normark/oop-csharp/html/notes/collections-note-time-complexity-dictionaries.html
I'd mention difference between dictionaries.
Above picture shows that
Dictionary<K,V>
is equal or faster in every case thanSorted
analog, but if order of elements is required, e.g. to print them,Sorted
one is chosen.Src: http://people.cs.aau.dk/~normark/oop-csharp/html/notes/collections-note-time-complexity-dictionaries.html
总结性能测试的结果 - SortedList、SortedDictionary、Dictionary、Dictionary。 Hashtable,不同场景下从最好到最差的结果:
内存使用:
插入:
搜索操作:
foreach 循环运营
To summarize the results of a Performance Test - SortedList vs. SortedDictionary vs. Dictionary vs. Hashtable, the results from best to worst for different scenarios:
Memory Usage:
Insertions:
Search Operations:
foreach loop operations
我可以看到建议的答案侧重于性能。下面提供的文章没有提供任何有关性能的新内容,但它解释了底层机制。另请注意,它并不关注问题中提到的三个
Collection
类型,而是解决System.Collections.Generic
命名空间的所有类型。http:// /geekswithblogs.net/BlackRabbitCoder/archive/2011/06/16/c.net-fundamentals-choosing-the-right-collection-class.aspx
摘录:
字典<>
排序字典<>
排序列表<>
基本过程的初步总结
非常欢迎反馈,因为我确信我没有把所有事情都做对。
n
。字典
内存
KeyArray(n) ->非排序数组<指针>
ItemArray(n) ->非排序数组<指针>
HashArray(n) ->排序数组
添加
HashArray(n) = Key.GetHash
# O(1)KeyArray(n) = PointerToKey # O(1)
ItemArray(n) = PointerToItem
# O(1)删除
对于 i = 0 到 n
,找到 < code>i whereHashArray(i) = Key.GetHash
# O(log n)(排序数组)HashArray(i)
# O(n) (排序数组)KeyArray(i)
# O(1)ItemArray(i)
# O(1)获取项目
对于 i = 0 到 n
,找到i
,其中HashArray(i) = Key.GetHash
# O(log n)(排序数组)ItemArray(i)
循环
对于 i = 0 到 n
,返回ItemArray(i)
SortedDictionary
内存
KeyArray(n) = 非排序数组<指针>
ItemArray(n) = 非排序数组<指针>
OrderArray(n) = 排序数组
Add
KeyArray(n) = PointerToKey
# O(1)ItemArray( n) = PointerToItem
# O(1)对于 i = 0 到 n
,找到i
,其中KeyArray(i-1)
键< KeyArray(i)
(使用ICompare
) # O(n)OrderArray(i) = n
# O(n) (排序数组)删除
对于 i = 0 到 n
,找到i
,其中KeyArray(i).GetHash = Key.GetHash
# O(n )KeyArray(SortArray(i))
# O(n)ItemArray(SortArray(i))
# O(n)OrderArray(i)< /code> # O(n)(排序数组)
获取项目
对于 i = 0 到 n
,找到i
其中KeyArray( i).GetHash = Key.GetHash
# O(n)ItemArray(i)
循环
For i = 0 到 n
, returnItemArray(OrderArray(i))
SortedList
内存
KeyArray(n) = 排序数组<指针>
ItemArray(n) = 排序数组
添加
对于 i = 0 到 n
,找到i
其中 <代码>KeyArray(i-1) <键< KeyArray(i) (使用ICompare
) # O(log n)KeyArray(i) = PointerToKey
# O(n)ItemArray( i) = PointerToItem
# O(n)删除
对于 i = 0 到 n
,找到i
其中KeyArray( i).GetHash = Key.GetHash
# O(log n)KeyArray(i)
# O(n)ItemArray(i)
# O( n)获取项目
对于 i = 0 到 n
,找到i
,其中KeyArray(i).GetHash = Key.GetHash
code> # O(log n)ItemArray(i)
循环
对于 i = 0 到 n
,返回ItemArray(i )
I can see the proposed answers focus on performance. The article provided below does not provide anything new regarding performance, but it explains the underlying mechanisms. Also note it does not focus on the three
Collection
Types mentioned in the question, but addresses all the Types of theSystem.Collections.Generic
namespace.http://geekswithblogs.net/BlackRabbitCoder/archive/2011/06/16/c.net-fundamentals-choosing-the-right-collection-class.aspx
Extracts:
Dictionary<>
SortedDictionary<>
SortedList<>
Tentative Summary of underlying Procedures
Feedback is very welcome as I am sure I did not get everything right.
n
.Dictionary
Memory
KeyArray(n) -> non-sorted array<pointer>
ItemArray(n) -> non-sorted array<pointer>
HashArray(n) -> sorted array<hashvalue>
Add
HashArray(n) = Key.GetHash
# O(1)KeyArray(n) = PointerToKey
# O(1)ItemArray(n) = PointerToItem
# O(1)Remove
For i = 0 to n
, findi
whereHashArray(i) = Key.GetHash
# O(log n) (sorted array)HashArray(i)
# O(n) (sorted array)KeyArray(i)
# O(1)ItemArray(i)
# O(1)Get Item
For i = 0 to n
, findi
whereHashArray(i) = Key.GetHash
# O(log n) (sorted array)ItemArray(i)
Loop Through
For i = 0 to n
, returnItemArray(i)
SortedDictionary
Memory
KeyArray(n) = non-sorted array<pointer>
ItemArray(n) = non-sorted array<pointer>
OrderArray(n) = sorted array<pointer>
Add
KeyArray(n) = PointerToKey
# O(1)ItemArray(n) = PointerToItem
# O(1)For i = 0 to n
, findi
whereKeyArray(i-1) < Key < KeyArray(i)
(usingICompare
) # O(n)OrderArray(i) = n
# O(n) (sorted array)Remove
For i = 0 to n
, findi
whereKeyArray(i).GetHash = Key.GetHash
# O(n)KeyArray(SortArray(i))
# O(n)ItemArray(SortArray(i))
# O(n)OrderArray(i)
# O(n) (sorted array)Get Item
For i = 0 to n
, findi
whereKeyArray(i).GetHash = Key.GetHash
# O(n)ItemArray(i)
Loop Through
For i = 0 to n
, returnItemArray(OrderArray(i))
SortedList
Memory
KeyArray(n) = sorted array<pointer>
ItemArray(n) = sorted array<pointer>
Add
For i = 0 to n
, findi
whereKeyArray(i-1) < Key < KeyArray(i)
(usingICompare
) # O(log n)KeyArray(i) = PointerToKey
# O(n)ItemArray(i) = PointerToItem
# O(n)Remove
For i = 0 to n
, findi
whereKeyArray(i).GetHash = Key.GetHash
# O(log n)KeyArray(i)
# O(n)ItemArray(i)
# O(n)Get Item
For i = 0 to n
, findi
whereKeyArray(i).GetHash = Key.GetHash
# O(log n)ItemArray(i)
Loop Through
For i = 0 to n
, returnItemArray(i)
当您希望在迭代集合时按键对集合进行排序时。如果您不需要对数据进行排序,那么最好只使用字典,它会具有更好的性能。
SortedList 和 SortedDictionary 几乎做同样的事情,但实现方式不同,因此具有不同的优点和缺点 在这里解释。
When you want the collection to be sorted by key when you iterate over it. If you don't need your data to be sorted, you're better off with just a Dictionary, it'll have better performance.
SortedList and SortedDictionary pretty much do the same thing, but are implemented differently, thus have different strengths and weaknesses explained here.
尝试为 @Lev 提出的每个案例分配性能分数,我使用了以下值:
结果是(越高=越好):
当然,每个用例都会给某些操作更多的权重。
Trying to assign a performance score to each case presented by @Lev, I used the following values:
The results are (higher = better):
Of course, every use-case will give more weight to certain operations.