问题描述
我有 60k 项需要对照 20k 查找列表进行检查.是否有一个集合对象(如 List
、HashTable
)提供了一个异常快速的 Contains()
方法?还是我必须自己写?换句话说,默认的 Contains()
方法是只扫描每个项目还是使用更好的搜索算法.
I have 60k items that need to be checked against a 20k lookup list. Is there a collection object (like List
, HashTable
) that provides an exceptionly fast Contains()
method? Or will I have to write my own? In otherwords, is the default Contains()
method just scan each item or does it use a better search algorithm.
foreach (Record item in LargeCollection)
{
if (LookupCollection.Contains(item.Key))
{
// Do something
}
}
注意.查找列表已经排序.
推荐答案
在最一般的情况下,考虑 System.Collections.Generic.HashSet
作为默认的包含"主力数据结构,因为评估 Contains
需要恒定的时间.
In the most general case, consider System.Collections.Generic.HashSet
as your default "Contains" workhorse data structure, because it takes constant time to evaluate Contains
.
什么是最快的可搜索集合"的实际答案取决于您的特定数据大小、有序性、散列成本和搜索频率.
The actual answer to "What is the fastest searchable collection" depends on your specific data size, ordered-ness, cost-of-hashing, and search frequency.
这篇关于什么 .NET 集合提供最快的搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!