C# o这项工作做得很好(可以使用where进行二进制搜索)。我对SortedList没有任何经验,也找不到任何关于linq与该集合的圈复杂度的信息。您可能需要对BinarySearch方法进行概要分析。若只搜索固定长度,字典似乎比列表快一点点。事实上,如果数据
C# o这项工作做得很好(可以使用where进行二进制搜索)。我对SortedList没有任何经验,也找不到任何关于linq与该集合的圈复杂度的信息。您可能需要对BinarySearch方法进行概要分析。若只搜索固定长度,字典似乎比列表快一点点。事实上,如果数据,c#,.net,database,in-memory-database,C#,.net,Database,In Memory Database,o这项工作做得很好(可以使用where进行二进制搜索)。我对SortedList没有任何经验,也找不到任何关于linq与该集合的圈复杂度的信息。您可能需要对BinarySearch方法进行概要分析。若只搜索固定长度,字典似乎比列表快一点点。事实上,如果数据本身很小,那么复制数据是个好主意。在线c#runner上的输出是:使用linq搜索列表。结果计数=1000 00:00:00.2860005填充字典00:00:00.2699203在字典中搜索。结果计数=1000 00:00:00.000088
o这项工作做得很好(可以使用
where
进行二进制搜索)。我对SortedList
没有任何经验,也找不到任何关于linq与该集合的圈复杂度的信息。您可能需要对BinarySearch方法进行概要分析。若只搜索固定长度,字典似乎比列表快一点点。事实上,如果数据本身很小,那么复制数据是个好主意。在线c#runner上的输出是:使用linq搜索列表。结果计数=1000 00:00:00.2860005填充字典00:00:00.2699203在字典中搜索。结果计数=1000 00:00:00.0000888
我不确定这是否有用。我的想法是,如果您的数据不经常更改,将它们存储在字典中(当然会有一些开销),并使用搜索条件作为键,将产生更快的搜索结果。
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
namespace CSharpConsoleApplication.Tests
{
class JustATest
{
public static void Run()
{
var list = new List<Test>();
for (int i = 0; i < 1000000; i++)
list.Add(new Test() { Text = "a" + i.ToString().PadLeft(6, '0') });
string input = "a011";
List<Test> found = null;
// Get the results with LinQ
var w = new Stopwatch(); w.Start();
found = list.Where(t => t.Text.Substring(0, 4) == input).ToList();
w.Stop();
Console.WriteLine("Search list with linq. Results count = {0}", found.Count);
Console.WriteLine(w.Elapsed);
Console.ReadLine();
// Store data in dictionary if no refresh needed
// Populate the dictionary
var objectsDictionary = new Dictionary<string, List<Test>>();
w.Restart();
PopulateDictionary(objectsDictionary, list, input.Length);
w.Stop();
Console.WriteLine("Populate dictionary");
Console.WriteLine(w.Elapsed);
Console.ReadLine();
// Search in dictionary
w.Restart();
if (objectsDictionary.ContainsKey(input))
found = objectsDictionary[input];
//objectsDictionary[input].ForEach(t => Console.WriteLine(t.Text));
w.Stop();
Console.WriteLine("Search in dictionary. Results count = {0}", found.Count);
Console.WriteLine(w.Elapsed);
Console.ReadLine();
}
static void PopulateDictionary(Dictionary<string, List<Test>> dictionary, List<Test> list, int textLength)
{
foreach (var t in list)
{
string text = t.Text.Substring(0, textLength);
if (dictionary.ContainsKey(text))
dictionary[text].Add(t);
else
dictionary.Add(text, new List<Test>() { t });
}
}
class Test
{
public string Text { get; set; }
}
}
}