简体   繁体   English

c#.net Compact Framework 3.5中最快的字典查找

[英]Fastest Dictionary lookup in c# .net Compact Framework 3.5

I'm looking for the fastest way to lookup if List, Set, Dictionary contains a specific Keyword (string). 我正在寻找最快的方法来查找List,Set,Dictionary是否包含特定的关键字(字符串)。 I don't need to store any data inside I just want to know if my Keyword is in the List. 我不需要存储任何数据,我只想知道我的关键字是否在列表中。

I thought about some possibilities like: 我想到了一些可能性:

Dictionary<string, bool> myDictionary = new Dictionary<string, bool>();
if (myDictionary.ContainsKey(valueToSearch))
{
    // do something
}

but I don't need a value. 但我不需要价值。

string[] myArray = {"key1", "key2", "key3"}
if (Array.IndexOf(myArray, valueToSearch) != -1)
{
    // do something
}

Then I found: 然后我发现:

List<string> list = new List<string>();
if (list.Contains(valueToSearch))    
{
    // do something
}

The lookup will happen very often and has to be very fast. 查找将经常发生,并且必须非常快。 Any idea what's the fastest way to check if a value equals one of a given list of keys? 知道什么是检查某个值是否等于给定键列表之一的最快方法?

Of the standard collection types, Dictionary will be the fastest, since I don't think you have HashSet<T> in the compact framework. 在标准集合类型中, Dictionary是最快的,因为我认为你在紧凑框架中没有HashSet<T> The other two do a sequential search. 另外两个进行顺序搜索。

In general, a Dictionary lookup is the usual solution to a problem like this, as long as your keys are good hash values that get a somewhat even distribution in the dictionary's lookup table. 通常,字典查找是这类问题的常用解决方案,只要您的键是良好的哈希值,在字典的查找表中得到一些均匀的分布。

However, there may be certain cases where a list lookup appears to run faster, depending on how the data is sorted and what exactly you are looking up. 但是,在某些情况下,列表查找似乎运行得更快,具体取决于数据的排序方式以及您正在查找的内容。

The best way to tell is to run a profile of each case, and see which performs better. 最好的方法是运行每个案例的个人资料,看看哪个表现更好。

I agree with Andy. 我同意安迪的观点。 You could also look at SortedList It's essentially a Dictionary that's sorted by its keys. 您还可以查看SortedList它本质上是一个按其键排序的字典。 Should make searching quicker if it's already sorted... 如果它已经排序,应该更快地搜索...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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