Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>One common mistake when using streams is to copy a byte at a time, or to use a small buffer. Most of the time it takes to write data to disk is spent seeking, so using a larger buffer will reduce your average seek time per byte.</p> <p>Operating systems write files to disk in clusters. This means that when you write a single byte to disk Windows will actually write a block between 512 bytes and 64 kb in size. You can get much better disk performance by using a buffer that is an integer multiple of 64kb.</p> <p>Additionally, you can get a boost from using a buffer that is a multiple of your CPUs underlying memory page size. For x86/x64 machines this can be set to either 4kb or 4mb.</p> <p>So you want to use an integer multiple of 4mb.</p> <p>Additionally if you use asynchronous IO you can fully take advantage of the large buffer size.</p> <pre><code>class Downloader { const int size = 4096 * 1024; ManualResetEvent done = new ManualResetEvent(false); Socket socket; Stream stream; void InternalWrite(IAsyncResult ar) { var read = socket.EndReceive(ar); if (read == size) InternalRead(); stream.Write((byte[])ar.AsyncState, 0, read); if (read != size) done.Set(); } void InternalRead() { var buffer = new byte[size]; socket.BeginReceive(buffer, 0, size, System.Net.Sockets.SocketFlags.None, InternalWrite, buffer); } public bool Save(Socket socket, Stream stream) { this.socket = socket; this.stream = stream; InternalRead(); return done.WaitOne(); } } bool Save(System.Net.Sockets.Socket socket, string filename) { using (var stream = File.OpenWrite(filename)) { var downloader = new Downloader(); return downloader.Save(socket, stream); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload