What is the best buffer size when using BinaryReader to read big files (>1 GB)?

后端 未结 2 602
梦谈多话
梦谈多话 2020-12-05 16:26

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         


        
2条回答
  •  情深已故
    2020-12-05 16:35

    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.

提交回复
热议问题