编写使用的StreamWriter比文件拷贝慢得多通过慢速网络到文件 [英] Writing to file using StreamWriter much slower than file copy over slow network

查看:289
本文介绍了编写使用的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.

推荐答案

如果您在使用默认的缓冲区大小,然后底层SMB协议将发行的块写入请求不超过4096字节较大,这意味着有大量往返在网络上创建你的StreamWriter 。你可以增加你的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天全站免登陆