以下两个代码示例在性能方面是否相等?
代码示例 1:
var count = 9999999999;
using(var sw = new StreamWriter())
{
for(int i=0;i<count;i++)
{
var result = SomeRelativeLongOperation(i);
sw.WriteLine(result);
}
}
代码示例 2:
var count = 9999999999;
var resultCollection = new ....
using(var sw = new StreamWriter())
{
for(int i=0;i<count;i++)
{
resultCollection.Add(SomeRelativeLongOperation(i));
if(resultCollection.Count%100==0)
{
WriteBlock(sw,resultCollection);
resultCollection.Clear();
}
}
}
我知道 Windows 使用内存缓冲区进行 IO 操作。所以,当我调用该StreamWriter.WriteLine
方法时,它首先将数据存储在内存中,然后刷新到硬盘驱动器,对吗?