为了账号安全,请及时绑定邮箱和手机立即绑定

SortedList和SortedDictionary有什么区别?

SortedList和SortedDictionary有什么区别?

烙印99 2019-08-30 16:22:31
a SortedList<TKey,TValue>和a 之间是否有任何实际的区别SortedDictionary<TKey,TValue>?在任何情况下你会专门使用一个而不是另一个吗?
查看完整描述

3 回答

?
三国纷争

TA贡献1804条经验 获得超7个赞

如果它有帮助,这是一个表格视图...


从绩效角度来看:


+------------------+---------+----------+--------+----------+----------+---------+

| Collection       | Indexed | Keyed    | Value  | Addition |  Removal | Memory  |

|                  | lookup  | lookup   | lookup |          |          |         |

+------------------+---------+----------+--------+----------+----------+---------+

| SortedList       | O(1)    | O(log n) | O(n)   | O(n)*    | O(n)     | Lesser  |

| SortedDictionary | n/a     | O(log n) | O(n)   | O(log n) | O(log n) | Greater |

+------------------+---------+----------+--------+----------+----------+---------+


* Insertion is O(1) for data that are already in sort order, so that each 

  element is added to the end of the list (assuming no resize is required).

从实施角度来看:


+------------+---------------+----------+------------+------------+------------------+

| Underlying | Lookup        | Ordering | Contiguous | Data       | Exposes Key &    |

| structure  | strategy      |          | storage    | access     | Value collection |

+------------+---------------+----------+------------+------------+------------------+

| 2 arrays   | Binary search | Sorted   | Yes        | Key, Index | Yes              |

| BST        | Binary search | Sorted   | No         | Key        | Yes              |

+------------+---------------+----------+------------+------------+------------------+

要大致套用,如果您需要原始性能SortedDictionary可能是一个更好的选择。如果您需要较少的内存开销,索引检索SortedList更适合。有关何时使用哪个,请参阅此问题。


查看完整回答
反对 回复 2019-08-30
  • 3 回答
  • 0 关注
  • 991 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信