.net DataTable.Select vs DataTable.rows.Find vs foreach vs Find(Predicate<T>)/Lambda
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/626679/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
DataTable.Select vs DataTable.rows.Find vs foreach vs Find(Predicate<T>)/Lambda
提问by Binoj Antony
I have a DataTable/collection that is cached in memory, I want to use this as a source to generate results for an auto complete textbox (using AJAX of course). I am evaluating various options to fetch the data quickly. The number of items in the collection/rows in the datatable could vary from 10000 to 2,000,000. (So that we dont get diverted, for the moment assume that the decision has been made, I have ample RAM and I will be using the cache and not database query for this)
我有一个缓存在内存中的数据表/集合,我想用它作为源来生成自动完成文本框的结果(当然使用 AJAX)。我正在评估各种选项以快速获取数据。数据表中集合/行中的项目数可能从 10000 到 2,000,000 不等。(这样我们就不会被转移,暂时假设已经做出决定,我有足够的 RAM,我将使用缓存而不是数据库查询)
I have some additional business logic for this processing; I have to prioritize the auto complete list as per a prioritycolumn (int) in the collection. So if I someone searches for Microand I get say 20 results of words/sentences that start with Microthen I would pick the top 10 resultant items with highest priority. (hence the need to have a priority property associated with the string value).
对于这个处理,我有一些额外的业务逻辑;我必须priority根据集合中的列 (int)对自动完成列表进行优先级排序。因此,如果我有人搜索Micro并得到 20 个以 开头的单词/句子的结果,Micro那么我将选择具有最高优先级的前 10 个结果项。(因此需要有一个与字符串值关联的优先级属性)。
The collection items are already sorted alphabetically.
收藏品已按字母顺序排序。
What would be the best solution in this case.
1. Using DataTable.Select(.
2. Using DataTable.Rows.Find(.
3. use a custom collection with foreach or forto iterate through its values.
4. use a generic collection with anonymous delegatesor lambda (since both give same performanceor not?)
在这种情况下最好的解决方案是什么。
1. 使用 DataTable.Select(.
2.使用 DataTable.Rows.Find(.
3. 使用带有foreach 或 for的自定义集合来迭代其值
。4 .使用带有匿名委托或 lambda的泛型集合(因为两者都给出相同的性能或不?)
回答by Jeff Certain
The charts aren't posted on my blog entry; more details can be found at http://msdn.microsoft.com/en-us/library/dd364983.aspx
这些图表没有发布在我的博客条目中;更多详细信息可以在http://msdn.microsoft.com/en-us/library/dd364983.aspx上找到
One other thing that I've since discovered is that, for large data sets, using a chained generic dictionary performs incredibly well. It also helps alleviate many of the issues caused by the sort operations required for aggregation operations such as min and max (either with DataTable.Computeor LINQ).
我后来发现的另一件事是,对于大型数据集,使用链式通用字典的性能非常好。它还有助于缓解由聚合操作所需的排序操作引起的许多问题,例如 min 和 max(使用DataTable.Compute或LINQ)。
By "chained generic dictionary," I mean a Dictionary(Of String, Dictionary(Of String, Dictionary(Of Integer, List(Of DataRow))))or similar technique, where the key for each dictionary is a search term.
我所说的“链式通用字典”是指一种Dictionary(Of String, Dictionary(Of String, Dictionary(Of Integer, List(Of DataRow))))或类似的技术,其中每个字典的关键字是一个搜索词。
Granted, this won't be useful in all circumstances, but I have at least one scenario where implementing this approach lead to a 500xperformance improvement.
当然,这不会在所有情况下都有用,但我至少有一个场景,实现这种方法可以500x提高性能。
In your case, I'd consider using a simple dictionary with the first 1-5 characters, then a List(Of String). You'd have to build up this dictionary once, adding the words to the lists with the first 1-5 characters, but after that you'll be able to get blazingly fast results.
在您的情况下,我会考虑使用前 1-5 个字符的简单字典,然后是List(Of String). 您必须构建一次该词典,将单词添加到包含前 1-5 个字符的列表中,但之后您将能够获得极快的结果。
I generally wrap things like this in a class that allows me to do things like add words easily. You may also want to use a SortedList(Of String), to get the results sorted automatically. This way, you can quickly look up the list of words that match the first N characters that have been typed.
我通常将这样的东西包装在一个类中,这样我就可以轻松地做一些事情,比如添加单词。您可能还想使用SortedList(Of String), 来自动对结果进行排序。这样,您可以快速查找与已键入的前 N 个字符匹配的单词列表。
回答by Michael Buen
On my autocomplete, i tried first the linq/lambdaapproach, the performance is a little slow. DataTable.Selectis faster than linq, so I use this. I haven't yet compared the performance between datatable.Selectand datatable.Find
在我的autocomplete,我首先尝试了这种linq/lambda方法,性能有点慢。DataTable.Select比 快linq,所以我用这个。我还没有比较之间的性能datatable.Select和datatable.Find
回答by James Orr
We could speculate about it all day, but since this is not a huge piece of code, why not write each one and benchmark them against each other?
我们可以猜测一整天,但是既然这不是一大段代码,为什么不编写每个代码并相互进行基准测试呢?
public delegate void TestProcedure();
public TimeSpan Benchmark(TestProcedure tp)
{
int testBatchSize = 5;
List<TimeSpan> results = new List<TimeSpan>();
for(int i = 0; i<testBatchSize; i++)
{
DateTime start = DateTime.Now;
tp();
results.Add(DateTime.Now - start);
}
return results.Min();
}
回答by James Orr
as per the following blog
根据以下博客
http://blog.dotnetspeech.net/archive/2008/08/26/performance----datatable.select-vs-dictionary.aspx
http://blog.dotnetspeech.net/archive/2008/08/26/performance----datatable.select-vs-dictionary.aspx
DataTable.Rows.Find is much, much faster than DataTable.Select.
DataTable.Rows.Find 比 DataTable.Select 快得多。
回答by Dillie-O
What about a DataView? You could apply your filter condition AND sort by the the priority, and easily iterate through the results to add to your results.
数据视图呢?您可以应用过滤条件并按优先级排序,然后轻松遍历结果以添加到结果中。

