dic*_*ice 6 .net c# collections performance
我有一个基本上是锯齿状的名称值对数组 - 我需要从中生成一组唯一的名称值.锯齿状阵列约为86,000 x 11值.对我来说,以何种方式存储名称值对(单个字符串"name = value"或专门的类,例如KeyValuePair)并不重要.
附加信息:有40个不同的名称和更多的不同值 - 可能在10,000个值的区域内.
我正在使用C#和.NET 2.0(并且性能非常差)我认为将整个锯齿状数组推送到sql数据库并从那里做一个不同的选择可能会更好.
以下是当前使用的代码:
List<List<KeyValuePair<string,string>>> vehicleList = retriever.GetVehicles();
this.statsLabel.Text = "Unique Vehicles: " + vehicleList.Count;
Dictionary<KeyValuePair<string, string>, int> uniqueProperties = new Dictionary<KeyValuePair<string, string>, int>();
foreach (List<KeyValuePair<string, string>> vehicle in vehicleList)
{
foreach (KeyValuePair<string, string> property in vehicle)
{
if (!uniqueProperties.ContainsKey(property))
{
uniqueProperties.Add(property, 0);
}
}
}
this.statsLabel.Text += "\rUnique Properties: " + uniqueProperties.Count;
Run Code Online (Sandbox Code Playgroud)
Bin*_*ier 12
我让它从9分钟以来的0.34秒内运行
问题是比较KeyValuePair结构.我通过编写比较器对象并将其实例传递给Dictionary来解决它.
从我可以确定的,KeyValuePair.GetHashCode()返回它的Key对象的哈希码(在这个例子中是最不唯一的对象).
当字典添加(并检查存在)每个项目时,它使用Equals和GetHashCode函数,但是当哈希码不太独特时必须依赖于Equals函数.
通过提供更独特的GetHashCode函数,它远远少于Equals函数.我还优化了Equals函数,以便在较少的unqiue键之前比较更独特的值.
使用下面的比较器对象,在0.34秒内运行86,000*11项具有10,000个唯一属性的项目(没有比较器对象需要9分22秒)
希望这可以帮助 :)
class StringPairComparer
: IEqualityComparer<KeyValuePair<string, string>>
{
public bool Equals(KeyValuePair<string, string> x, KeyValuePair<string, string> y)
{
return x.Value == y.Value && x.Key == y.Key;
}
public int GetHashCode(KeyValuePair<string, string> obj)
{
return (obj.Key + obj.Value).GetHashCode();
}
}
Run Code Online (Sandbox Code Playgroud)
编辑:如果它只是一个字符串(而不是KeyValuePair,其中string = Name + Value),它将大约快两倍.这是一个很好的有趣的问题,我花了很多时间(虽然我学会了安静)