a SortedList<TKey,TValue>和a 之间是否有任何实际的区别SortedDictionary<TKey,TValue>?在任何情况下你会专门使用一个而不是另一个吗?
我发现SortedList<TKey, TValue> SortedDictionary<TKey, TValue>并Dictionary<TKey, TValue>实现相同的接口.
SortedList还是SortedDictionary结束Dictionary?SortedList和SortedDictionary应用方面?这似乎与这个问题重复,后者询问" SortedList和SortedDictionary之间有什么区别?" 不幸的是,答案只是引用MSDN文档(它清楚地表明两者之间存在性能和内存使用差异),但实际上并没有回答这个问题.
事实上(根据MSDN,这个问题没有得到相同的答案):
的
SortedList<TKey, TValue>通用类是O(log n)的检索,其中n是字典中的元件的数目的二进制搜索树.在这里,它类似于SortedDictionary<TKey, TValue>泛型类.这两个类具有相似的对象模型,并且都具有O(log n)检索.两个类别的不同之处在于内存使用和插入和移除速度:
SortedList<TKey, TValue>使用的内存少于SortedDictionary<TKey, TValue>.
SortedDictionary<TKey, TValue>对于未排序的数据,O(log n)具有更快的插入和删除操作,而不是O(n)SortedList<TKey, TValue>.如果列表是从排序数据中一次性填充的,
SortedList<TKey, TValue>则速度快于SortedDictionary<TKey, TValue>.
所以,显然这表明这SortedList<TKey, TValue>是更好的选择,除非您需要更快的插入和删除未排序数据的操作.
问题仍然存在,鉴于以上信息,使用a的实际(现实世界,商业案例等)原因是SortedDictionary<TKey, TValue>什么?根据绩效信息,这意味着根本没有必要SortedDictionary<TKey, TValue>.
这是像这样的问题的延续.
是否有任何调整性能的指导原则?我并不是指大O的收益,只是节省一些线性时间.
例如,要花多少钱预分类保存在任SortedList或SortedDictionary?
假设我有一个有3个属性的人类排序,其中一个是年龄.我应该先按年龄换取物品吗?
我应该首先对一个属性进行排序,然后使用结果列表/字典对两个属性进行排序,依此类推?
想到的任何其他优化?
在Python 2.6中是否有内置的排序字典实现,或者哈希表是唯一的类型?
正如我在上一篇文章中写的那样,我对c#世界还是一个新手,所以我写了一个小基准来比较Dictionary,Hashtable,SortedList和SortedDictionary.该测试运行8000次迭代,从50到100000个元素.我测试了添加新元素,搜索元素并循环遍历一些元素.结果就像我预期的那样,除了SortedDictionary的结果,这让我感到很困惑......所有结果都很慢.所以我错过了关于排序字典概念的一些内容.我已经问了谷歌,但我发现的所有其他人都得到了相同的测试结果.根据他们的测试实施略有不同.我的问题再次出现:为什么SortedDicrionary比其他所有人慢得多?
我有包含键值对的字典.
SortedDictionary<int,int> dictionary=new SortedDictionary<int,int>();
dictionary.Add(1,33);
dictionary.Add(2,20);
dictionary.Add(4,35);
Run Code Online (Sandbox Code Playgroud)
我想从已知的键值获取先前的键值对.在上面的例子中,如果我有键4,那我怎么能得到<2,20>?
我在互联网上看到了几个关于此的引用,但没有官方文档?谁能告诉我在哪里可以获得有关此信息?
我很难将自定义IComparer用于我的SortedDictionary <>.目标是将电子邮件地址以特定格式(firstnam.lastname@domain.com)作为密钥,并按姓氏排序.当我做这样的事情时:
public class Program
{
public static void Main(string[] args)
{
SortedDictionary<string, string> list = new SortedDictionary<string, string>(new SortEmailComparer());
list.Add("a.johansson@domain.com", "value1");
list.Add("b.johansson@domain.com", "value2");
foreach (KeyValuePair<string, string> kvp in list)
{
Console.WriteLine(kvp.Key);
}
Console.ReadLine();
}
}
public class SortEmailComparer : IComparer<string>
{
public int Compare(string x, string y)
{
Regex regex = new Regex("\\b\\w*@\\b",
RegexOptions.IgnoreCase
| RegexOptions.CultureInvariant
| RegexOptions.IgnorePatternWhitespace
| RegexOptions.Compiled
);
string xLastname = regex.Match(x).ToString().Trim('@');
string yLastname = regex.Match(y).ToString().Trim('@');
return xLastname.CompareTo(yLastname);
}
}
Run Code Online (Sandbox Code Playgroud)
我得到这个ArgumentException:
An entry with the same …
从SortedSet<T>.GetEnumerator文档:
该方法是O(1)操作
从SortedDictionary<K, V>.GetEnumerator文档:
此方法是O(log n)操作,其中n是count.
这两个陈述都可以是真的,考虑到SortedDictionary<K, V>内部实现为SortedSet<KeyValuePair<K, V>?我检查了类的GetEnumerator代码SortedDictionary- 它直接使用了SortedSet枚举器.我注意到了SortedSet枚举器的实现,在我看来它确实有O(log n)特性(这里是代码):
public SortedSet<T>.Enumerator GetEnumerator()
{
return new SortedSet<T>.Enumerator(this);
}
//which calls this constructor:
internal Enumerator(SortedSet<T> set)
{
this.tree = set;
this.tree.VersionCheck();
this.version = this.tree.version;
this.stack = new Stack<SortedSet<T>.Node>(2 * SortedSet<T>.log2(set.Count + 1));
this.current = (SortedSet<T>.Node) null;
this.reverse = false;
this.siInfo = (SerializationInfo) null;
this.Intialize();
}
private void Intialize()
{
this.current = (SortedSet<T>.Node) null;
SortedSet<T>.Node …Run Code Online (Sandbox Code Playgroud) sorteddictionary ×10
c# ×7
.net ×6
sortedlist ×4
dictionary ×3
generics ×2
benchmarking ×1
big-o ×1
binary-tree ×1
icomparer ×1
linq ×1
performance ×1
python ×1
python-2.6 ×1
sortedset ×1
sorting ×1
treemap ×1