简体   繁体   中英

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

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

使用SortedList类,然后使用.BinarySearch()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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