我目前正在測試將文本數據記錄到文件中的不同方法的性能。看來,當我打開/寫入/關閉很多次時,使用的擴展名會影響性能。 (.txt和.LOG是約7倍的速度)爲什麼文件擴展名會影響寫入速度? (C#,StreamWriter)
代碼使用:
private static void TestWriteSpeed(FileInfo file)
{
Stopwatch watch = new Stopwatch();
watch.Start();
for (int i = 0; i < 5000; i++)
{
using (StreamWriter writer = file.AppendText())
{
writer.Write("This is a test");
}
}
Console.WriteLine(file.Name + ": " + watch.Elapsed);
}
static void Main(string[] args)
{
TestWriteSpeed(new FileInfo("abc.txt"));
TestWriteSpeed(new FileInfo("abc.txt.01564611564"));
TestWriteSpeed(new FileInfo("abc.01564611564.txt"));
TestWriteSpeed(new FileInfo("abc.xml"));
TestWriteSpeed(new FileInfo("abc.xml.01564611564"));
TestWriteSpeed(new FileInfo("abc.config"));
TestWriteSpeed(new FileInfo("abc.config.01564611564"));
TestWriteSpeed(new FileInfo("abc.exe"));
TestWriteSpeed(new FileInfo("abc.exe.01564611564"));
TestWriteSpeed(new FileInfo("abc.log"));
TestWriteSpeed(new FileInfo("abc.log.01564611564"));
Console.ReadLine();
}
結果:
abc.txt 00:00:08.3826847 <---
abc.txt.01564611564 00:00:59.7401633
abc.01564611564.txt 00:00:08.0069698 <---
abc.xml 00:00:58.2031820
abc.xml.01564611564 00:00:59.3956204
abc.config 00:00:58.4861308
abc.config.01564611564 00:01:01.2474287
abc.exe: 00:01:00.0924401
abc.exe.01564611564 00:01:00.7371805
abc.log 00:00:08.0009934 <---
abc.log.01564611564 00:00:59.8029448
這究竟是爲什麼?
殺毒軟件已關閉,我希望? – 2010-07-09 15:02:31
@orsol誰會運行AV而不是RANU? – Will 2010-07-09 15:06:04
@Will什麼是RANU? – 2010-07-09 15:13:26