Robocopy 背后的算法是什么?

发布于 2024-09-28 15:20:32 字数 198 浏览 6 评论 0 原文

我很想知道是什么让 Robocopy(强大的文件复制)如此快速和强大。有人知道 Robocopy 使用的 API/算法是什么吗?有人学过Robocopy吗?

我之所以这么问,是因为我必须编写一个方法(在 .NET/C# 中),它将快速且无错误地复制目录/文件...数据量可能高达 15Gb,并且由于各种原因我不能简单地调用 Robocopy。

谢谢!

I am curious to know what makes Robocopy (Robust File Copy) so fast and robust. Any body knows what is the API/Algo used for Robocopy? Anybody studied Robocopy?

I am asking since I have to write a method (in .NET/C#) which will copy directories/files fast and without errors... The amount of data can go up to 15Gb and I cannot simply call Robocopy for various reasons.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

伪装你 2024-10-05 15:20:32

通过一个简单的 C# 程序,您可以非常接近 Robocopy 的速度,该程序使用具有 64K 缓冲区的标准 FileStream 进行异步读取和写入。更大的缓冲区大小(高达 256K)将略微提高性能。大于 256K 会使速度减慢到令人惊讶的程度。在我的测试中,使用 512K 缓冲区复制所需的时间几乎是使用 256K 缓冲区复制的时间的两倍。

这个想法非常简单:

Read the first buffer from the source file
do
{
    start asynchronous write to destination file.
    Read the next buffer from the source file
    wait for asynchronous write to complete
} while not end of file

编写起来非常简单。我的程序执行此操作几乎与 Robocopy 一样快,并且不会导致 当您从服务器复制非常大(数百GB)的文件时,Robocopy 导致的问题

有关大文件复制问题的更多信息

请注意,如果您在同一物理磁盘上进行读取和写入,则这种异步读/写操作不会对性能产生太大影响。当源和目标位于不同的驱动器上时,它是最有效的。

You can get very close to Robocopy's speed with a simple C# program that does asynchronous reads and writes using a standard FileStream with a 64K buffer. Larger buffer sizes up to 256K will give a slight performance increase. Larger than 256K will slow things down to a surprising extent. In my tests, using a 512K buffer took almost twice as long as copying with a 256K buffer.

The idea is pretty simple:

Read the first buffer from the source file
do
{
    start asynchronous write to destination file.
    Read the next buffer from the source file
    wait for asynchronous write to complete
} while not end of file

It's a pretty simple thing to write. My program that does this is almost as fast as Robocopy and doesn't cause the kinds of problems that Robocopy causes when you're copying a very large (hundred gigabyte) file from a server.

A bit more info on the large file copy problem.

Note that this asynchronous read/write thing doesn't do much for performance if you're reading from and writing to the same physical disk. It's most effective when the source and destination are on different drives.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文