Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determining buffer size when working with files in C#? [duplicate]

Tags:

c#

.net

io

c#-4.0

I have this simple code which combines text files into one text file :

void Main()
{
const int chunkSize = 2 * 1024; // 2KB
var inputFiles = new[] { @"c:\1.txt", @"c:\2.txt", @"c:\3.txt" };
using (var output = File.Create(@"c:\output.dat"))
{
    foreach (var file in inputFiles)
    {
        using (var input = File.OpenRead(file))
        {
            var buffer = new byte[chunkSize];
            int bytesRead;
            while ((bytesRead = input.Read(buffer, 0, buffer.Length)) > 0)
            {
                output.Write(buffer, 0, bytesRead);
            }
        }
    }
}
}

My question is about the chunkSize size.

How can I know if the number I've chosen is the right one ? (1024*2)

I'm trying to find the idle formula :

Assuming each file size is F mb , and I have R mb of Ram and the block size of my Hd is B kb - is there any formula which I can build to find the idle buffer size ?

like image 852
Royi Namir Avatar asked Aug 13 '13 11:08

Royi Namir


People also ask

How do you determine buffer size?

To check the buffer window, multiply the bit rate (bits per second) by the buffer window (in seconds) and divide by 1000 to get the size, in bits, of the buffer for the stream.

What is the typical size of the file buffer?

The disk buffer is usually quite small, ranging between 8 and 256 MiB, and the page cache is generally all unused main memory.

Why is buffer size 1024?

1024 is the exact amount of bytes in a kilobyte. All that line means is that they are creating a buffer of 16 KB. That's really all there is to it. If you want to go down the route of why there are 1024 bytes in a kilobyte and why it's a good idea to use that in programming, this would be a good place to start.

What is buffer size in C#?

The default buffer size is 4096. 0 or 1 means that buffering should be disabled. Negative values are not allowed. public: property int BufferSize { int get(); void set(int value); }; C# Copy.


1 Answers

4KB is a good choice. for more info look to this:
File I/O with streams - best memory buffer size

Greetings

like image 127
Bassam Alugili Avatar answered Oct 06 '22 00:10

Bassam Alugili