将文件保存到网络

发布于 2024-07-20 05:09:58 字数 734 浏览 6 评论 0原文

我有一个大约 7 MB 的文件,只需几秒钟即可保存到我的本地共享。 但是,将该文件保存到网络位置需要分钟。 我想知道我能做些什么来加快速度。 以下是我当前的选项:

  1. 将数据保存到本地计算机上的临时文件,然后将该临时文件复制到网络路径。 我可能会这样做,因为这是最简单、最划算的。
  2. 使用SetFilePointerEx()SetEndOfFile()。 我认为根据这个问题的答案这可能很有用:在 Windows 上创建大文件
  3. 缓冲区写入。 我可以自己缓存写入数据并在缓冲区满时刷新,但这对于操作系统已经完成的缓存来说不是多余的吗?

#1 似乎是最好的选择,但我想知道是否有人对加速保存到网络路径的更好方法有任何建议?

编辑:网络位于千兆位 LAN 上,因此速度不应该成为问题。 将文件复制到网络路径大约需要 1 秒。 我只是注意到我们在较小的数据块上调用 WriteFile() ,所以我们可能应该这样做,因此优化更高级别的代码以写入更大的数据块可能会有所帮助,但速度差异仍然如此之大,以至于这仍然是一个值得提出的问题。

I have a file that's about 7 MB that saves to my local share in a matter of seconds. However, saving that file to a network location takes minutes. I'm wondering what I can do to speed this up. Here are my current options:

  1. Save the data to a temporary file on the local machine, then copy the temporary file over to the network path. I'll probably do this as this is the easiest and the most bang for the buck.
  2. Use SetFilePointerEx() and SetEndOfFile(). I thought this might be useful based on the answer to this question: Creating big file on Windows
  3. Buffer writes. I could cache write data myself and flush when the buffer full, but wouldn't this be redundant with the caching that is already done by the OS?

#1 seems like the best option, but I'm wondering if anyone has any advice on a better way to speed up saving to network paths?

Edit: The network is on a gigabit LAN, so speed shouldn't be an issue. Copying the file to the network path takes about 1 second. I just noticed we're calling WriteFile() on smaller chunks of data then we probably should, so optimizing the higher level code to write bigger chunks will probably help, but the speed difference is still so significant that it's still a worthwhile question to ask.

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

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

发布评论

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

评论(3

无法回应 2024-07-27 05:09:58

我想知道是否有人对加速保存到网络路径的更好方法有任何建议?

也许您需要更好的网络。 ISP 通常提供快速下载,但上传缓慢。 使用 FTP 等协议传输 7 MB 需要多长时间?

I'm wondering if anyone has any advice on a better way to speed up saving to network paths?

Maybe you need a better network. ISPs often provide fast downloads but slow uploads. How long does it take to transfer 7 MB using a protocol such as FTP?

风透绣罗衣 2024-07-27 05:09:58

您需要避免读取-修改-写入操作。 您通常希望写入至少 4KB 的块,可能是 2 的更高次方。原因是要附加一个字节,您通常需要读取文件的最后一个块,附加一个字节,然后写回新块。 通过(仅)写入 4KB 块,每次写入通常都会在文件末尾作为一个新块结束。

缓存应该可以帮助您,但缓存并不完美。 以独占方式打开文件可能会有所帮助。 如果您拒绝读取访问,操作系统可能会注意到刷新缓存对于其他应用程序来说并不是太重要。

CopyFile 可以很快,因为它可以做完全相同的事情。

You'll want to aovid read-modify-write operations. You'll typically want to write blocks of at least 4KB, possibly higher powers of 2. The reason is that to append one byte, you usually need to read the last block of a file, append one byte, and write back the new block. By writing 4KB blocks (only), every write typically ends up as a new block at the end of the file.

Caching should help you here, but caching isn't perfect. It may help to open the file exclusively. If you deny read access, the OS might notice that flushing the cache isn't too important for other apps.

CopyFile can be fast because it can do exactly the same.

冬天旳寂寞 2024-07-27 05:09:58

您是否在慢速网络上运行?

Id 选择选项 1 并将文件保存到后台的网络共享中

Are you running on a slow network?

Id go with option number 1 and save the file to the network share in the background

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