搜索列表< string>用于字符串.StartsWith() [英] search List<string> for string .StartsWith()
问题描述
我有一个
List<string>
具有1500个字符串.我现在使用以下代码仅提取以prefixText开头的字符串.
with 1500 strings. I am now using the following code to pull out only string that start with the string prefixText.
foreach(string a in <MYLIST>)
{
if(a.StartsWith(prefixText, true, null))
{
newlist.Add(a);
}
}
这是非常快的,但是我正在快速地寻找google.现在我的问题是,如果我按字母顺序排列列表,然后逐个字符比较字符,我可以使其更快吗?或有任何其他建议可以加快速度?
This is pretty fast, but I'm looking for google fast. Now my question is if I arrange the List in alphabetical order, then compare char by char can I make this faster? Or any other suggestions on making this faster?
推荐答案
1500通常太少了:
1500 is usually too few:
-
您可以使用简单的分治法并行搜索它.在两个不同的作业/线程中搜索列表的每一半(或分为三个,四个,...,部分).
you could search it in parallel with a simple divide and conquer of the problem. Search each half of the list in two (or divide into three, four, ..., parts) different jobs/threads.
或将字符串存储在(非二进制)树中.将是O(log n).
Or store the strings in a (not binary) tree instead. Will be O(log n).
按字母顺序排序,您可以执行二进制搜索(与上一个相同)
sorted in alphabetical order you can do a binary search (sort of the same as the previous one)
这篇关于搜索列表< string>用于字符串.StartsWith()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!