我想优化这段代码:
public static void ProcessTo(this StreamReader sr, StreamWriter sw, Action<StreamWriter, string> action, FileProcessOptions fpo = null)
{
if (fpo == null)
{
fpo = new FileProcessOptions();
}
List<string> buffer = new List<string>(fpo.BuferSize);
while (!sr.EndOfStream)
{
buffer.Clear();
while (!sr.EndOfStream && buffer.Count < fpo.BuferSize)
{
buffer.Add(sr.ReadLine());
}
if (fpo.UseThreads)
{
buffer.AsParallel().ForAll(line => action(sw, line));
}
else
{
buffer.ForEach(line => action(sw, line));
}
}
}
我处理大量数据并希望并行化该过程。通常数据归档,因此使用多线程处理数据流非常重要