gpt4 book ai didi

c# - 检查 File.Exists() 是否提高写入速度

转载 作者:太空狗 更新时间:2023-10-29 19:44:07 24 4
gpt4 key购买 nike

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

可能有很多文件,因此,我想衡量影响会有多少开销(就时间而言)。因此,我创建了一个控制台应用程序来对其进行测试。

我的代码

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

最佳答案

太多的代码无法放在评论中 - 简短的回答是 Exists + Write 通常应该比只写花费更长的时间(即使对于现有文件)。

磁盘 IO 不是很可预测(缓存、预热、机器负载、IO 队列、HDD/SSD 模型等),但运行大量迭代(超过 1000 次)的测试需要超过几毫秒应该给你和想法。在我的机器上,Exists+Write 通常需要更长的时间,但也有异常(exception) - 它可能是页面交换干扰或其中一个 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);
}
}

自己检查一下,看看它在您的机器上的表现如何。顺便说一句:在 for 循环中使用 i++; 是没有意义的。

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

关于c# - 检查 File.Exists() 是否提高写入速度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19654861/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com