Writing to file using StreamWriter much slower than file copy over slow network

前端 未结 2 1135
死守一世寂寞
死守一世寂寞 2020-12-11 18:24

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.

Using the following

相关标签:
2条回答
  • 2020-12-11 18:53

    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))
    

    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.

    0 讨论(0)
  • 2020-12-11 19:11

    I'm not a security expert, but in my opinion, this is most related to permission issue.

    Everytime you write somethin to a disk on remote server the permission is going to be checked against the user that execution that action. The permission to write, naturally.

    In case instead of the copy, that control is executed only ones. So it's much faster.

    In fact, to prove this you can try to copy several files over the network and after copy ZIP files (with the same amount of memory in zipped state) over network. In second case it will be much faster, as permission control is executed only once, for single ZIP file.

    Hope this helps.

    0 讨论(0)
提交回复
热议问题