简体   繁体   English

C#循环优化

[英]C# loop optimization

i have this loop and it loops for large count like 30 000 times at least 我有这个循环,它至少循环大数,如30 000次
i am looking for some way to improve it's performance 我正在寻找一些方法来改善它的性能
DbRecordDictionary is derived from DictionaryBase class DbRecordDictionary派生自DictionaryBase类
here is the loop: 这是循环:

ArrayList noEnter = new ArrayList();
DbRecordDictionary oldArray = new DbRecordDictionary();
DbRecordDictionary userArray = new DbRecordDictionary();
DbRecordDictionary result = null;
foreach (string key in keys)
{
    if (noEnter.Contains(key))
    { //may need cast!
        if (count < 1)
            result.Add(key, userArray[key]);
        else if (oldArray.Count == 0)
            break;
        else if (oldArray.Contains(key))
            result.Add(key, userArray[key]);
    }                
}

You may want to use a Dictionary/Hashset for oldArray , but else there is not much you can do. 您可能需要使用词典/对于的Hashset oldArray ,但其他人没有什么可以做。 Also noEnter if that is an array. 如果是数组,也noEnter

From what I can see, the variable count or the oldArray never changes during the loop, so you can place those condition outside the loop, and make two different loops. 从我所看到的,变量countoldArray在循环期间永远不会改变,因此你可以将这些条件放在循环之外,并制作两个不同的循环。

if (count < 1) {
  foreach (string key in keys) {
    if (noEnter.Contains(key)) {
      result.Add(key, userArray[key]);
    }
  }
} else if (oldArray.Count == 0) {
  // no data
} else {
  foreach (string key in keys) {
    if (noEnter.Contains(key)) {
      if (oldArray.Contains(key)) {
        result.Add(key, userArray[key]);
      }
    }                
  }
}

The collections noEnter and oldArray should be dictionaries, otherwise you will be spending a lot of execution time in the Contains calls. 集合noEnteroldArray应该是字典,否则你将在Contains调用中花费大量的执行时间。

If noEnter has more then about 10 items in it, then use a Dictionary rathern then a List/Array for it. 如果noEnter中有超过10个项目,则使用Dictionary而不是List / Array。 As a Dictionary can look up a item without having to look at all the items, when an List/Array has to loop over all items. 当List / Array必须循环遍历所有项目时,Dictionary可以查找项目而无需查看所有项目。

Otherwise consider shorting "keys" and "oldArray" and then proforming a "merge" on them. 否则考虑缩短“keys”和“oldArray”,然后在它们上形成“合并”。 Look at the code for a "merge sort" to see how to do the merge. 查看“合并排序”的代码,了解如何进行合并。 (This would need carefull profiling) (这需要仔细分析)

try this 尝试这个

(from k in keys
     where noEnter.Contains(k) &&
           oldArray.Count > 0 &&
           count < 1 &&
           oldArray.Contains(k)
     select k)
        .ToList()
        .ForEach(k => result.Add(k, userArray[k]));

Small small optimization could be using generics ArrayList noEnter = new ArrayList(); 小的小优化可能是使用泛型ArrayList noEnter = new ArrayList(); would be List noEnter = new List(); 将是List noEnter = new List(); and DbRecordDictionary would inherit Dictonary instead of DictionaryBase. 和DbRecordDictionary将继承Dictonary而不是DictionaryBase。

im not 100% sure that you would gain performance but you will use a more modern c# style. 我不是100%肯定你会获得性能,但你会使用更现代的c#风格。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM