簡體   English   中英

如何提高此代碼的性能?

[英]How can I improve the performance of this code?

在我嘗試從字符串列表(_authorizedBks)中搜索字典列表(tr)時,我是否可以提高此代碼的性能? 有沒有更好的方法在C#中編寫代碼或在.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>> 

如果你想搜索那些你可以嘗試HashSet<T> 在散列集中的搜索在O(1)處攤銷。

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM