2010-07-09 46 views
11

我目前正在測試將文本數據記錄到文件中的不同方法的性能。看來,當我打開/寫入/關閉很多次時,使用的擴展名會影響性能。 (.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 

這究竟是爲什麼?

+14

殺毒軟件已關閉,我希望? – 2010-07-09 15:02:31

+2

@orsol誰會運行AV而不是RANU? – Will 2010-07-09 15:06:04

+0

@Will什麼是RANU? – 2010-07-09 15:13:26

回答

15

看起來像另一個應用程序或進程正在讀取或監視正在編寫的文件並忽略.txt或.log文件出於性能原因。

爲什麼?因爲你在我的筆記本電腦上運行一堆代碼,所有文件(22秒)的結果都一樣,沒有任何變化。

+0

哇,我永遠不會有關於我的防病毒,thx的快速回答 – 2010-07-09 15:31:24

5

正如Orsol建議,您的AV可能會忽略txt和日誌文件。

6

我在我的工作機器上測試了這個;安裝了帶有Symantec Endpoint Protection AV的32位Windows XP的Core 2計算機。這些是我的結果:

abc.txt:    00:00:07.1192029 
abc.txt.01564611564: 00:00:06.9956377 
abc.01564611564.txt: 00:00:06.9534773 
abc.xml:    00:00:06.9368894 
abc.xml.01564611564: 00:00:07.9326258 
abc.config:    00:00:07.9074675 
abc.config.01564611564: 00:00:08.0205423 
abc.exe:    00:00:21.2559372 
abc.exe.01564611564: 00:00:07.2417322 
abc.log:    00:00:07.0871043 
abc.log.01564611564: 00:00:07.1848522 

在我的情況下,只有.exe擴展需要更長的時間。

所以,有人猜測,防病毒會干擾寫入速度。

編輯:我應該注意到,這個用戶是AD域的有限用戶。

相關問題