Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么文件扩展名会影响写入速度?(C#,StreamWriter)_C#_Streamwriter - Fatal编程技术网

为什么文件扩展名会影响写入速度?(C#,StreamWriter)

为什么文件扩展名会影响写入速度?(C#,StreamWriter),c#,streamwriter,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 (St

我目前正在测试将文本数据记录到文件中的不同方法的性能。似乎当我打开/写入/关闭大量次时,使用的扩展会影响性能。(.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();
}
private static void TestWriteSpeed(FileInfo文件)
{
秒表=新秒表();
watch.Start();
对于(int i=0;i<5000;i++)
{
使用(StreamWriter=file.AppendText())
{
writer.Write(“这是一个测试”);
}
}
Console.WriteLine(file.Name+“:”+watch.appeased);
}
静态void Main(字符串[]参数)
{
TestWriteSpeed(新文件信息(“abc.txt”);
TestWriteSpeed(新文件信息(“abc.txt.01564611564”);
TestWriteSpeed(新文件信息(“abc.01564611564.txt”);
TestWriteSpeed(新文件信息(“abc.xml”);
TestWriteSpeed(新文件信息(“abc.xml.01564611564”);
TestWriteSpeed(新文件信息(“abc.config”);
TestWriteSpeed(新文件信息(“abc.config.01564611564”);
TestWriteSpeed(新文件信息(“abc.exe”);
TestWriteSpeed(新文件信息(“abc.exe.01564611564”);
TestWriteSpeed(新文件信息(“abc.log”);
TestWriteSpeed(新文件信息(“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

abc.txt 00:00:08.3826847正如Orsol所建议的,您的AV可能会忽略txt和日志文件。

看起来像是另一个应用程序或进程正在读取或监视正在写入的文件,并出于性能原因忽略.txt或.log文件


为什么??因为你的一堆代码在我的笔记本电脑上运行时,对所有文件都给出相同的结果(22秒),没有任何变化。

我在我的工作机器上测试了这一点;安装了Symantec Endpoint Protection AV的32位Windows XP核心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扩展需要更长的时间

因此,是的,猜测一下,反病毒正在干扰写入速度


编辑:我应该注意,该用户是广告域上的有限用户。

我希望防病毒软件已关闭?@orsol谁会运行AV而不是RANU?@Will:嘿,猜猜看。。。微软每月推送的更新?有些修复操作系统漏洞是可以利用的,即使当前使用该机器的用户不是管理员。这充其量是对AV的补充,而不是替代。哇,我永远不会想到我的防病毒软件,thx的快速回答