使用 StreamWriter 写入文件比通过慢速网络复制文件慢得多 [英] Writing to file using StreamWriter much slower than file copy over slow network

查看:26
本文介绍了使用 StreamWriter 写入文件比通过慢速网络复制文件慢得多的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序试图将大量文​​本写入海外远程服务器上的文件,该服务器的网络连接速度很慢.

I have a program that attempts to write quite a large amount of text to a file on a remote server overseas, which has a slow network connection.

使用以下代码,其中 outputFileContent 是一个 StringBuilder:

Using the following code, where outputFileContent is a StringBuilder:

using (var outfile = new StreamWriter(myRemoteFilePath))
{
    outfile.Write(outputFileContent.ToString());
}

运行时间很长(几分钟),而如果我先写入本地文件,然后将其复制到远程位置,速度会快得多(20-30 秒):

it is taking a seriously long time to run (several minutes), whereas if I first write to a local file and then copy it across to the remote location, it is much quicker (20-30 secs):

string tempFilePath = Path.GetTempFileName();
using (var outfile = new StreamWriter(tempFilePath))
{
    outfile.Write(outputFileContent.ToString());
}

System.IO.File.Copy(tempFilePath, myRemoteFilePath, true)

知道为什么会这样吗?我唯一的猜测是这与网络缓冲有关,或者可能是因为流编写器不知道它需要提前多大.

Any idea why this is happening? My only guess is that it is something to do with buffering across the network, or perhaps because the stream writer doesn't know how big it needs to be ahead of time.

推荐答案

如果您使用默认缓冲区大小创建 StreamWriter,那么底层 SMB 协议将以不超过 4096 字节的块发出写入请求,这意味着大量网络往返.您可以将 StreamWriter 的缓冲区大小增加到最大 64k,以减少往返次数:

If you create your StreamWriter using default buffer sizes then the underlying SMB protocol will be issuing write requests in chunks no larger than 4096 bytes, which means a large number of round-trips over the network. You could increase your StreamWriter's buffer size up to a maximum of 64k in order to reduce the number of round trips:

using (var outfile = new StreamWriter(myRemoteFilePath, false, Encoding.ASCII, 0x10000))

将缓冲区大小增加到 64k 以上在任何情况下都无济于事,因为底层 SMB 协议不支持缓冲区长度超过 64k.请注意,直接文件复制仍然使用 SMB 协议,因此从网络流量的角度来看,除了缓冲区大小之外,操作之间几乎没有区别.

Increasing the buffer size beyond 64k will not help in any circumstance, as the underlying SMB protocol does not support buffer lengths beyond 64k. Note that a direct file copy still uses the SMB protocol, so from a network traffic perspective there's little difference between the operations except for the buffer sizes.

这篇关于使用 StreamWriter 写入文件比通过慢速网络复制文件慢得多的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆