c# - SortedList 和 SortedDictionary 之间有什么区别?

标签 c# .net generics sortedlist sorteddictionary

SortedList<TKey,TValue> 之间有什么真正的实际区别吗?和一个 SortedDictionary<TKey,TValue> ?在某些情况下,您会特别使用其中一种而不使用另一种吗?

最佳答案

是的——它们的性能特征差异很大。称它们为 SortedList 可能会更好和 SortedTree因为这更密切地反射(reflect)了实现。

查看它们各自的 MSDN 文档( SortedList SortedDictionary ),了解不同情况下不同操作的性能详细信息。这是一个很好的总结(来自 SortedDictionary 文档):

The SortedDictionary<TKey, TValue> generic class is a binary search tree with O(log n) retrieval, where n is the number of elements in the dictionary. In this, it is similar to the SortedList<TKey, TValue> generic class. The two classes have similar object models, and both have O(log n) retrieval. Where the two classes differ is in memory use and speed of insertion and removal:

  • SortedList<TKey, TValue> uses less memory than SortedDictionary<TKey, TValue>.

  • SortedDictionary<TKey, TValue> has faster insertion and removal operations for unsorted data, O(log n) as opposed to O(n) for SortedList<TKey, TValue>.

  • If the list is populated all at once from sorted data, SortedList<TKey, TValue> is faster than SortedDictionary<TKey, TValue>.

(SortedList实际上维护的是一个排序数组,而不是使用树。它仍然使用二进制搜索来查找元素。)

关于c# - SortedList 和 SortedDictionary 之间有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/935621/

相关文章:

c# - 从模板向 C# WinForms 应用程序添加新选项卡?

C# 计时器与 Datetime.Now 同步

c# - Web 请求和响应的 cookies 处理

c# - SignalR for .net 和观察者模式

c# - 如何在 Windows Phone 7 中突出显示上下文相关搜索的结果?

.net - .NET 中的双向数据绑定(bind)不起作用,即使使用 INotifyPropertyChanged 对象也是如此

c# - async/await WhenAll 在两个具有相同返回类型的方法上

java - Java 中泛型、类型、通配符之间的区别

c# - 通用类文件名约定

generics - Kotlin 中的使用站点差异