我正在尝试优化搜索大文本文件(300-600mb)中的字符串.使用我目前的方法,它需要太长时间.
目前我一直在使用IndexOf搜索字符串,但是花费的时间太长(20秒)来为每个字符串构建一个索引.
如何优化搜索速度?我试过Contains()但这也很慢.有什么建议?我正在考虑正则表达式匹配,但我没有看到有显着的速度提升.也许我的搜索逻辑是有缺陷的
例
while ((line = myStream.ReadLine()) != null)
{
if (line.IndexOf(CompareString, StringComparison.OrdinalIgnoreCase) >= 0)
{
LineIndex.Add(CurrentPosition);
LinesCounted += 1;
}
}
Run Code Online (Sandbox Code Playgroud)