除了dictionary.add之外,还有没有更快的方法在C#dictionary中添加行?
我们正在使用字典和键值对作为字符串,字典已经在foreach循环中添加了数百万行。探查器结果显示dictionary.add以分钟为单位计算百万次以上点击所花费的时间 以下是供参考的方法除了dictionary.add之外,还有没有更快的方法在C#dictionary中添加行?,c#,dictionary,C#,Dictionary,我们正在使用字典和键值对作为字符串,字典已经在foreach循环中添加了数百万行。探查器结果显示dictionary.add以分钟为单位计算百万次以上点击所花费的时间 以下是供参考的方法 public virtual Dictionary<string, string> CreateDictionaryRow(string[] row) { Dictionary<string, string> dictionaryRow = new Dictionary<s
public virtual Dictionary<string, string> CreateDictionaryRow(string[] row)
{
Dictionary<string, string> dictionaryRow = new Dictionary<string, string>();
foreach (ColumnMappingBO item in this.Layout.Items)
{
string value = string.Empty;
if (!string.IsNullOrEmpty(item.ColumnPosition))
{
if (item.ColumnPosition == "ZZ")
{
value = string.Empty;
}
else
{
if (LayoutPosition.TryGetValue(item.ColumnID, out Ordinal))
{
if (row.Length > Ordinal)
{
if (row[Ordinal] != null)
{
value = row[Ordinal];
}
}
}
}
}
dictionaryRow.Add(item.ColumnNameID, value);
}
return dictionaryRow;
}
公共虚拟字典CreateDictionaryRow(字符串[]行)
{
Dictionary dictionaryRow=新字典();
foreach(此.Layout.Items中的ColumnMappingBO项)
{
字符串值=string.Empty;
如果(!string.IsNullOrEmpty(item.ColumnPosition))
{
如果(item.ColumnPosition==“ZZ”)
{
value=string.Empty;
}
其他的
{
if(LayoutPosition.TryGetValue(item.ColumnID,out序号))
{
如果(行长度>序号)
{
if(行[序号]!=null)
{
值=行[序号];
}
}
}
}
}
dictionaryRow.Add(item.ColumnNameID,值);
}
返回窗口;
}
所有代码行都很好,执行dictionaryRow.Add()时会影响性能。让我们知道如何优化向字典添加行,以及可以使用的任何外部自定义库?我会尝试将其更改为LINQ查询 也许是这样的:
public Dictionary<string, string> CreateDictionaryRow(string[] row)
{
return this.Layout.Items.ToDictionary(item => item.ColumnNameID,
item => GetValue(item, row));;
}
private string GetValue(ColumnMappingBO item, string[] row)
{
string value = string.Empty;
if (!string.IsNullOrEmpty(item.ColumnPosition))
{
if (item.ColumnPosition == "ZZ")
{
value = string.Empty;
}
else
{
if (LayoutPosition.TryGetValue(item.ColumnID, out Ordinal))
{
if (row.Length > Ordinal)
{
if (row[Ordinal] != null)
{
value = row[Ordinal];
}
}
}
}
}
return value;
}
公共字典CreateDictionaryRow(字符串[]行)
{
返回this.Layout.Items.ToDictionary(item=>item.ColumnNameID,
item=>GetValue(item,row));;
}
私有字符串GetValue(ColumnMappingBO项,字符串[]行)
{
字符串值=string.Empty;
如果(!string.IsNullOrEmpty(item.ColumnPosition))
{
如果(item.ColumnPosition==“ZZ”)
{
value=string.Empty;
}
其他的
{
if(LayoutPosition.TryGetValue(item.ColumnID,out序号))
{
如果(行长度>序号)
{
if(行[序号]!=null)
{
值=行[序号];
}
}
}
}
}
返回值;
}
要试验的一件事是item.ColumnNameId生成多少个唯一的hashcodes。
如果存在大量冲突,则基本上与在linkedlist上迭代相同,直到到达末尾,然后在末尾添加对象
下面是调用dictionary.Add()时调用的代码,该代码取自
有了大量的冲突,你就开始循环和比较键,有了这么大的字典,你可能会遇到一些冲突
void Insert(TKey key, TValue value, bool add) {
if (buckets == null) Initialize(0);
int hashCode = comparer.GetHashCode(key) & 0x7FFFFFFF;
int targetBucket = hashCode % buckets.Length;
for (int i = buckets[targetBucket]; i >= 0; i = entries[i].next) {
if (entries[i].hashCode == hashCode && comparer.Equals(entries[i].key, key)) {
entries[i].value = value;
version++;
return;
}
}
一些有散列、存储桶和大小调整的东西。这些操作需要时间。为什么你认为可以做得更快?你为什么用字典?你试过了吗?我不认为是.Add方法减慢了你的速度-它所做的只是在字典中添加了2个字符串,所以这不是你问题的根源。。。初始化和调用dictionary.Add之间的所有代码可能需要改进您是否考虑过将密钥类型更改为比字符串更有效的类型?前几天,我遇到了一个类似的问题,一本字典中键入了远程对象。在我的例子中,将键更改为字符串可以极大地提高性能(快100多倍)。谢谢Jon。我的意思是每次调用字典。加法(),如果我们考虑0.015Ms并乘以数百万的点击量,它会在几分钟内增加时间。键类型为string。除了.add(),还有其他解决方案吗?@chint除了添加()之外,没有其他解决方案,但除了您添加的内容,还有其他解决方案。一个包含几个字符的字符串键的效率要低于某些整数标识符。你能把钥匙换成这样吗?谢谢米洛斯,性能是一样的。如果还有什么可以测试,请告知。