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