Copying from one stream to another?

前端 未结 4 2096
伪装坚强ぢ
伪装坚强ぢ 2021-02-09 03:07

For work, the specification on my project is to use .Net 2.0 so I don\'t get the handy CopyTo function brought about later on.

I need to copy the response s

4条回答
  •  逝去的感伤
    2021-02-09 03:53

    The size of the buffer does matter. If you're copying a megabyte of data one byte at a time, for example, you're going to make a 2^20 iterations through the loop. If you're copying 1 kilobyte at a time, you'll only make 2^10 iterations through the loop. There is significant overhead in the calls to Read and Write when you're making a million of them.

    For reading FileStream, I typically use a buffer that's between 64K and 256K. Anything less than 32K shows a marked decrease in performance, as does anything above 256K. The difference between using a 64K buffer and a 256K buffer is not worth the extra memory. Be aware, though, that those numbers are on my system and across my network. Your numbers will vary depending on hardware and operating system.

    For network streams, you should select a buffer size that will keep up with the incoming data stream. I'd suggest at least 4 kilobytes, which will give you some buffer if the write stalls for any reason.

提交回复
热议问题