替换嵌套的foreach以提高性能 [英] Replace nested foreach to Increase performance

查看:58
本文介绍了替换嵌套的foreach以提高性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我有以下代码需要更长的时间来执行。有任何替代方案,我可以用LINQ或任何其他方式替换下面的代码提高性能?

Hi,

I have the following code which take longer time to execute.Is there any alternative that i can replace below code with LINQ or any other way to increase the performance?

var usedLists = new HashSet<long>();
foreach (var test in Tests)
{
  var requiredLists = this.GetLists(test.test, selectedTag);

  foreach (var List in requiredLists)
  {   
    if (!usedLists.Contains(List.Id))
    {
      usedLists.Add(List.Id);
      var toRecipients = List.RecepientsTo;
      var ccRecipients = List.RecipientsCC;
      var bccRecipients = List.RecipientsBCC;
      var replyTo = new List<string>() { List.ReplyTo };
      var mailMode = isPreviewMode ? MailMode.Display : MailMode.Drafts;
      OutlookModel.Instance.CreateEmail(toRecipients, ccRecipients, bccRecipients, this.Draft, mailMode, replyTo);
    }
  }
}



提前致谢。



谢谢,

Prasant


Thanks in advance.

Thanks,
Prasant

推荐答案

循环没有什么错误的性能。



我预计性能问题会出现在CreateEmail部分。正如佐罗所说,如果这可以并行化,那么这可能会有所帮助。 GetLists需要多长时间?



我非常怀疑你会使用LINQ对性能进行任何改进。
There's nothing really wrong performance wise with the looping.

I'd expect the performance problems come in the CreateEmail part. As Zorro suggests, if this can be parallelized then that might help. How long does GetLists take?

I very much doubt you'll make any improvements in performance using LINQ.


先测量瓶颈是什么。

只考虑优化。



优化还可能包括替代算法,例如如果创建一个电子邮件是一个很大的成本,它可能是一个收集所有数据并向所有人发送一封电子邮件的解决方案吗?

干杯

Andi
Measure first what the bottle-neck is.
Only then contemplate on optimization.

Optimization might also include alternative algorithms, e.g. if creating an email is the big cost, might it be a solution to collect all data and send one email to all?
Cheers
Andi


这篇关于替换嵌套的foreach以提高性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆