我有一个大小约为1GB的.txt文件(包含超过一百万行),同时有一个字符串列表,我正在尝试从文件中删除存在于字符串列表中的所有行,并创建新文件,但这需要非常长的时间。
using (StreamReader reader = new StreamReader(_inputFileName))
{
using (StreamWriter writer = new StreamWriter(_outputFileName))
{
string line;
while ((line = reader.ReadLine()) != null)
{
if (!_lstLineToRemove.Contains(line))
writer.WriteLine(line);
}
}
}
如何提高我的代码性能?
_lstLineToRemove
从List<string>
转换为HashSet<string>
(假设它还不是哈希集合)。 - ghordList<String>
替换为HashSet<String>
。 - OliverHashSet<string>
替换List<string>
可能会有很大帮助,特别是如果_lstLineToRemove
包含数百行。 - Matthew Watson