【问题标题】:Why does file extension affect write speed? (C#, StreamWriter)为什么文件扩展名会影响写入速度? (C#,流编写器)
【发布时间】:2011-03-13 22:24:03
【问题描述】:

我目前正在测试将文本数据记录到文件中的不同方法的性能。似乎当我打开/写入/关闭大量时间时,使用的扩展名会影响性能。 (.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

为什么会这样?

【问题讨论】:

  • 我希望杀毒软件已关闭?
  • @orsol 谁会运行 AV 而不仅仅是 RANU?
  • @Will:嘿,猜猜看……微软每个月都会推送的那些更新?即使当前使用机器的用户不是管理员,其中一些修复了可利用的操作系统漏洞。
  • 我相信 RANU = 以普通用户身份运行(例如msdn.microsoft.com/en-us/library/bb932482(VS.90).aspx)。这充其量是对 AV 的补充,而不是替代。

标签: c# streamwriter


【解决方案1】:

我在我的工作机器上对此进行了测试;一台装有 32 位 Windows XP 并安装了 Symantec Endpoint Protection AV 的 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 域上的受限用户。

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

    • 哇,我永远不会想到我的防病毒软件,谢谢您的快速回答
    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-31
      • 1970-01-01
      • 1970-01-01
      • 2018-04-18
      相关资源
      最近更新 更多