![](/img/trans.png)
[英]WPF C# StreamWriter does not write to file and raises no exception
[英]Why does file extension affect write speed? (C#, StreamWriter)
我目前正在測試將文本數據記錄到文件中的不同方法的性能。 似乎當我打開/寫入/關閉很多次時,使用的擴展會影響性能。 (.txt和.log快〜7倍)
使用的代碼:
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
為什么會這樣?
看起來其他應用程序或進程正在讀取或監視正在寫入的文件,並出於性能原因忽略.txt或.log文件。
為什么? 因為在我的筆記本電腦上運行時,您的一堆代碼會為所有文件(22秒)提供相同的結果,而不會有任何變化。
我在我的工作機器上測試了這個; 安裝了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域上的受限用戶。
正如Orsol建議的那樣,你的AV可能會忽略txt和日志文件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.