Click here to Skip to main content
15,939,783 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

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))
      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);

Thanks in advance.

Updated 20-May-14 1:29am
_Zorro_ 20-May-14 9:01am    
Have you tried using parallel.foreach in your second foreach? Since you are only sending emails it shouldn't be a problem to parallelize your iteration.
Ziee-M 20-May-14 12:57pm    
Hi, i don't think there is any EASY/SIMPLE optimazation (talking about the loops). In the other hand, if the CreateEmail sends emails,then yes you can optimise your solution.

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.
Share this answer
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?
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900