2013-09-01 54 views
0

我可以提高此代碼的性能,我試圖從字符串列表(_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>> 

回答

2

如果你想搜索那些你可以給HashSet<T>試試嗎?哈希集中的搜索在O(1)處攤銷。

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

確保您構建authorizedBks用正確的IComparer,雖然。 –

+0

此代碼是Web服務的一部分,並且可能有用於此Web服務的C++或Java客戶端,那麼Hashtable可串行化並可由C++或Java客戶端使用? – user2736702

+0

@ user2736702是的。 –