这是对我之前的问题Log to memory and then write to file的引用,实际上是该问题的编辑部分,我在编辑部分询问,如果我写入内存会比写入文件更快吗?我进行了一次简单的测试,结果令人震惊!我想与社区分享。 所以这是代码
private void Button1Click(object sender, EventArgs e)
{
var stopwatch = new Stopwatch();
stopwatch.Start();
File.AppendAllText(@"D:\File1.txt", string.Format("{0}Start! : {1}", Environment.NewLine, DateTime.Now.ToString(CultureInfo.InvariantCulture)));
for (int i = 0; i < 6; i++)
{
for (int j = 0; j < 1000000; j++)
{
File.AppendAllText(@"D:\File1.txt", string.Format("{0}{1}:{2}", Environment.NewLine, i.ToString(CultureInfo.InvariantCulture), j.ToString(CultureInfo.InvariantCulture)));
}
}
File.AppendAllText(@"D:\File1.txt", string.Format("{0}Done!{1}", Environment.NewLine, DateTime.Now.ToString(CultureInfo.InvariantCulture)));
stopwatch.Stop();
File.AppendAllText(@"D:\File1.txt",
string.Format("{0}{1}:{2}",Environment.NewLine, stopwatch.Elapsed.ToString(), stopwatch.ElapsedMilliseconds.ToString(CultureInfo.InvariantCulture)));
MessageBox.Show("Done!");
}
private void Button2Click(object sender, EventArgs e)
{
var stopwatch = new Stopwatch();
using (var mem = new MemoryStream())
{
using (var binaryWriter = new BinaryWriter(mem))
{
stopwatch.Start();
{
binaryWriter.Write("start! : " + DateTime.Now.ToString(CultureInfo.InvariantCulture));
for (int i = 0; i < 6; i++)
{
for (int j = 0; j < 1000000; j++)
{
binaryWriter.Write(i.ToString(CultureInfo.InvariantCulture) + ":" + j.ToString(CultureInfo.InvariantCulture));
}
}
stopwatch.Stop();
binaryWriter.Write("Done! " + DateTime.Now.ToString(CultureInfo.InvariantCulture));
binaryWriter.Write(stopwatch.Elapsed.ToString() + ":" + stopwatch.ElapsedMilliseconds.ToString(CultureInfo.InvariantCulture));
binaryWriter.Flush();
var file = new FileStream(@"D:\File2.txt", FileMode.Create);
mem.WriteTo(file);
}
}
}
MessageBox.Show("Done!");
}
因为代码应该易于理解
Elapsed time in File1.txt = 00:50:24.5654918
Elapsed milliseconds in File1.txt = 3024565
Elapsed time in File2.txt = 00:00:04.7430152
Elapsed milliseconds in File2.txt = 4743
所以,正如你自己可以看到的那样,大约有50分钟的差异!如果你把所有内容直接记录到IO文件而不使用内存,这可能是导致性能不佳的真正原因流或任何用于日志记录的自定义工具,OTOH 与50分钟的File.AppendAllText相比,使用MemoryStream只花了大约4和四分之一秒 。 (我仍然感到困惑的是,为什么windows explorer中显示的时间与stopwatch.ShowElapasedTime最终在文件中显示的时间不对应,但是,即使我们看到Windows资源管理器时间,它仍然是大约45分钟更快!)所以,这可能是一个非常有用的东西,我想分享它!
答案 0 :(得分:2)
那是因为File.AppendAllText打开文件,写入,刷新缓冲区并关闭它。如果你保持日志文件打开并使用流写入它(而不是MemoryStream),你将得到的结果非常接近你在MemoryStream中看到的结果 - 它甚至可能无法区分。
尝试一下。