在SortedList<TKey,TValue>和SortedDictionary<TKey,TValue>之间有任何真正的实际区别吗?在某些情况下,你会特别使用其中一种而不是另一种吗?
当前回答
是的,它们的表现特征有很大不同。将它们命名为SortedList和SortedTree可能更好,因为这样可以更准确地反映实现。
查看它们各自的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实际上维护一个排序的数组,而不是使用树。它仍然使用二进制搜索来查找元素。)
其他回答
是的,它们的表现特征有很大不同。将它们命名为SortedList和SortedTree可能更好,因为这样可以更准确地反映实现。
查看它们各自的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实际上维护一个排序的数组,而不是使用树。它仍然使用二进制搜索来查找元素。)
查看SortedList的MSDN页面:
备注部分:
The SortedList<(Of <(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 SortedDictionary<(Of <(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<(Of <(TKey, TValue>)>) uses less memory than SortedDictionary<(Of <(TKey, TValue>)>). SortedDictionary<(Of <(TKey, TValue>)>) has faster insertion and removal operations for unsorted data, O(log n) as opposed to O(n) for SortedList<(Of <(TKey, TValue>)>). If the list is populated all at once from sorted data, SortedList<(Of <(TKey, TValue>)>) is faster than SortedDictionary<(Of <(TKey, TValue>)>).
索引访问(这里提到)是实际的区别。 如果需要访问后继对象或前任对象,则需要SortedList。SortedDictionary无法做到这一点,因此您在如何使用排序(first / foreach)方面受到相当的限制。
这是性能之间相互比较的可视化表示。
关于这个话题已经说得够多了,但是为了简单起见,这里是我的看法。
排序字典应该使用时-
需要更多的插入和删除操作。 无序数据。 键访问就足够了,不需要索引访问。 内存不是瓶颈。
另一方面,当-时应该使用Sorted List
需要更多的查找和更少的插入和删除操作。 数据已经排序(如果不是全部,也是大部分)。 需要索引访问。 内存是一种开销。
希望这能有所帮助!!
推荐文章
- HTTP POST返回错误:417“期望失败。”
- 如何在。net中创建和使用资源
- 为什么Path。以Path.DirectorySeparatorChar开头的文件名合并不正确?
- 如何在c#中获得正确的时间戳
- Linq选择列表中存在的对象(A,B,C)
- c# .NET中的App.config是什么?如何使用它?
- c#:如何获得一个字符串的第一个字符?
- String类中的什么方法只返回前N个字符?
- 我如何提高ASP。NET MVC应用程序性能?
- 更好的方法将对象转换为int类型
- 我可以将c#字符串值转换为转义字符串文字吗?
- 在c#中转换char到int
- c#中朋友的对等物是什么?
- 关键字使用virtual+override vs. new
- 无法解析类型为“Microsoft.AspNetCore.Http.IHttpContextAccessor”的服务