I\'m reading binary files and here is a sample:
public static byte[] ReadFully(Stream input)
{
byte[] buffer = new byte[16*1024];
int read;
while
There is no best or worst buffer size, but you have to look at the some aspects.
As you are using C#, so you run on Windows, Windows uses NTFS and its page size is 4 MB, so it is advisable to use multiples of 4096. So your buffer size is 16*1024 = 4*4096
, and it is a good choice, but to say if it is better or worse than 16*4096
we cannot say.
Everything depends on the situation and the requirements for program. Remember here you cannot choose the best option, but only some better. I recommend to use 4096
, but also you could use your own 4*4096
or even 16*4096
, but remember, that this buffer will be allocated on the heap, so its allocation takes some time, so you don't want to allocate a big buffer, for example 128*4096
.
"Sequential File Programming Patterns and Performance with .NET" is a great article in I/O performance improvement.
In page 8 of this PDF file, it shows that the bandwidth for buffer size bigger than eight bytes, is constant. Consider that the article has been written in 2004 and the hard disk drive is "Maxtor 250 GB 7200 RPM SATA disk" and the result should be different by latest I/O technologies.
If you are looking for the best performance take a look at pinvoke.net or the page 9 of the PDF file, the un-buffered file performance measurements shows better results:
In un-buffered I/O, the disk data moves directly between the application’s address space and the device without any intermediate copying.