检查File.Exists()是否提高了写入速度

时间:2013-10-29 09:55:37

标签: c#

我的应用程序将一些文件写入光盘,但我意识到在此过程中我正在编写现有文件。所以,我需要先检查文件是否存在,然后执行一些逻辑。

可能会有很多文件,因此,我想要估算出影响会有多大(超过时间)。所以,我创建了一个控制台应用程序来测试它。

我的代码

using System;
using System.Collections.Generic;
using System.IO;

namespace TimeForFileRead
{
    class Program
    {
        static string myPath = "C:\\Users\\DRook\\Desktop\\temp\\";
        static string myPathFile = myPath + "file";
        static void Main(string[] args)
        {
            for (int i = 0; i < 5; i++)
            {
                DoSomeWork();
                Console.WriteLine(" =  =  =  =  =  =============== =  =  =  =  =");
            }
            Console.ReadKey();
        }

        static void DoSomeWork()
        {
            if (!Directory.Exists(myPath))
                Directory.CreateDirectory(myPath);    

            System.Diagnostics.Stopwatch stopWatch = new System.Diagnostics.Stopwatch();

            stopWatch.Start();

            for (int i = 0; i < 1000; i++)
            {
                using (StreamWriter sw = new StreamWriter(myPathFile + i.ToString() + ".txt"))
                {
                    sw.Write(i.ToString());
                }
                i++;
            }

            stopWatch.Stop();

            Console.WriteLine("Write only: " + stopWatch.Elapsed);

            Directory.Delete(myPath, true);
            System.Threading.Thread.Sleep(500);
            Directory.CreateDirectory(myPath);
            System.Threading.Thread.Sleep(500);

            stopWatch.Reset();

            stopWatch.Start();

            for (int i = 0; i < 1000; i++)
            {
                if (!File.Exists(myPathFile + i.ToString() + ".txt"))
                {
                    using (StreamWriter sw = new StreamWriter(myPathFile + i.ToString() + ".txt"))
                    {
                        sw.Write(i.ToString());
                    }
                }
                i++;
            }
            stopWatch.Stop();
            Console.WriteLine("Write and File check: " + stopWatch.Elapsed);
        }
    }
}

因此,正如您所看到的,它执行了2个操作。我正在写文件到磁盘,另一个是检查文件是否已经存在,如果没有,则写入光盘。

我的控制台窗口的屏幕截图(结果):

enter image description here

正如您所看到的,奇怪的是,首先检查文件是否存在然后写入它比直接写入光盘几乎总是更快。这让我很困惑。当然这没有意义。为什么这个额外的头部提高速度(考虑到File.Exists()总是在我的代码中返回false,因此不跳过Write)?我假设我的代码有问题,但我已经看了一段时间,我无法理解它。

修改

根据评论,我稍微改变了顺序,所以我现在先执行File.Exists()检查,然后再写。结果更夸张(虽然我现在根据上面的代码迭代超过10000而不是1000):

enter image description here

修改2

@MatthewWatson注意到我的代码有问题,我已更新它以确保首先删除目录。同样的问题仍然存在,但发生率大大降低,但速度上却出现了更大的差异。

using System;
using System.Collections.Generic;
using System.IO;

namespace TimeForFileRead
{
    class Program
    {
        static string myPath = "C:\\Users\\DRook\\Desktop\\temp\\";
        static string myPathFile = myPath + "file";
        static void Main(string[] args)
        {
            for (int i = 0; i < 5; i++)
            {
                DoSomeWork();
                Console.WriteLine(" =  =  =  =  =  =============== =  =  =  =  =");
            }
            Console.ReadKey();
        }

        static void DoSomeWork()
        {
            if (Directory.Exists(myPath))
                Directory.Delete(myPath, true);

            Directory.CreateDirectory(myPath);

            System.Diagnostics.Stopwatch stopWatch = new System.Diagnostics.Stopwatch();

            stopWatch.Start();

            for (int i = 0; i < 10000; i++)
            {
                using (StreamWriter sw = new StreamWriter(myPathFile + i.ToString() + ".txt"))
                {
                    sw.Write(i.ToString());

                }
                i++;
            }

            stopWatch.Stop();

            Console.WriteLine("Write  took : " + stopWatch.Elapsed);

            Directory.Delete(myPath, true);
            System.Threading.Thread.Sleep(500);
            Directory.CreateDirectory(myPath);
            System.Threading.Thread.Sleep(500);

            stopWatch.Reset();

            stopWatch.Start();

            for (int i = 0; i < 10000; i++)
            {
                if (!File.Exists(myPathFile + i.ToString() + ".txt"))
                {
                    using (StreamWriter sw = new StreamWriter(myPathFile + i.ToString() + ".txt"))
                    {
                        sw.Write(i.ToString());
                    }
                }
                i++;
            }

            stopWatch.Stop();

            Console.WriteLine("Write and check took: " + stopWatch.Elapsed);
        }
    }
}

enter image description here

2 个答案:

答案 0 :(得分:2)

要添加注释的代码太多 - 简短的回答是Exists + Write通常需要比写入更长的时间(即使对于现有文件)。

磁盘IO不是很容易预测(缓存,预热,机器负载,IO队列,HDD / SSD模型等),但运行大量迭代(超过1000次)的测试需要花费几毫秒的时间应该给你和想法。在我的机器上,Exists + Write通常需要更长时间,但也有例外 - 它可能是页面交换干扰或其中一个VM,谁知道....

这是一个略有修改的测试套件,有4个场景: 1.新文件夹,只写 2.新文件夹,存在+写 3.现有文件夹和文件(从步骤2开始)只写 4.现有文件夹和文件(来自步骤2)存在+写入

以下代码:

class FTest
{
    static string myPath = "C:\\Users\\DRook\\Desktop\\temp\\";
    static string myPathFile = myPath + "file";

    public static void test()
    {
        for (int i = 0; i < 5; i++)
        {
            DoSomeWork();
            Console.WriteLine(" =  =  =  =  =  =============== =  =  =  =  =");
        }
        Console.ReadKey();
    }

    public static void testX1(string path, int index)
    {
        using (StreamWriter sw = new StreamWriter(path + index.ToString() + ".txt"))
        {
            sw.Write(index.ToString());
        }
    }

    public static void testX2(string path, int index)
    {
        if (!File.Exists(path + index.ToString() + ".txt"))
        {
            using (StreamWriter sw = new StreamWriter(path + index.ToString() + ".txt"))
            {
                sw.Write(index.ToString());
            }
        }
        else
        {
            using (StreamWriter sw = new StreamWriter(path +"n"+ index.ToString() + ".txt"))
            {
                sw.Write(index.ToString());
            }
        }
    }

    static void runTestMeasure(Action<string, int> func, int count, string message, bool cleanup)
    {
        if (cleanup)
        {
            if (Directory.Exists(myPath)) Directory.Delete(myPath, true);
            System.Threading.Thread.Sleep(500);
            Directory.CreateDirectory(myPath);
            System.Threading.Thread.Sleep(500);
        }

        System.Diagnostics.Stopwatch stopWatch = new System.Diagnostics.Stopwatch();

        stopWatch.Start();

        for (int i = 0; i < count; i++)
        {
            func(myPath,i);
        }

        stopWatch.Stop();

        Console.WriteLine(message+": " + stopWatch.Elapsed);
    }

    static void DoSomeWork()
    {
        int count = 10000;
        runTestMeasure((path, ndx) => { testX1(path, ndx); },count,"Write missing file",true);
        System.Threading.Thread.Sleep(5000);
        runTestMeasure((path, ndx) => { testX2(path, ndx); }, count, "Write+Exists missing file",true);
        System.Threading.Thread.Sleep(5000);
        runTestMeasure((path, ndx) => { testX2(path, ndx); }, count, "Write existing file", false);
        System.Threading.Thread.Sleep(5000);
        runTestMeasure((path, ndx) => { testX2(path, ndx); }, count, "Write+Exists existing file", false);
    }
}

检查自己并查看它在您的计算机上的行为方式。 顺便说一句:在循环中使用i++;毫无意义。

编辑:修复textX2代码以创建新文件(备用名称),如果文件存在

答案 1 :(得分:1)

您的测试没有热身,而且您将Exists置于时间之外。我想当你使用相同的文件时,它可以在操作系统或硬件级别上缓存。为了使这个测试更好:

  • 添加预热
  • 为每次运行使用随机/唯一文件名
  • 使用1000和10000和100000个文件进行测试
  • 确保您的gc在每次测试开始时处于相同状态