user2736702
user2736702

Reputation: 33

How can I improve the performance of this code?

Can I improve the performance of this code where in I am trying to search a List of Dictionary (tr) from a List of string (_authorizedBks). Is there any better way to code this in C# or supporting languages in .NET?

for (int i = tr.Count - 1; i >= 0; i--)
{
     if (tr[i].ContainsKey("BK") && !_authorizedBks.Contains(tr[i]["BK"], StringComparer.CurrentCultureIgnoreCase))
     {
          removedBks.Add(tr[i]);
     }
}

// where tr is List<Dictionary<string, string>> 
// _authorizedBks is List<string>
// removedBks is List<Dictionary<string, string>> 

Upvotes: 0

Views: 123

Answers (2)

Tim P.
Tim P.

Reputation: 2942

Use a SortedList class, and then use .BinarySearch()

Upvotes: 0

Bhushan Firake
Bhushan Firake

Reputation: 9458

If you want to search in those you can give HashSet<T> a try? The search in a hashset is amortized at O(1).

 HashSet<Dictionary<string, string>> tr = new HashSet<Dictionary<string, string>>();
 HashSet<string> _authorizedBks = new HashSet<string>();

Upvotes: 2

Related Questions