与文件在C#中工作时,确定缓冲区的大小? [英] Determining buffer size when working with files in C#?

查看:412
本文介绍了与文件在C#中工作时,确定缓冲区的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个简单的code结合文本文件合并为一个文本文件:

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);
            }
        }
    }
}
}

我的问题是关于 CHUNKSIZE 尺寸。

我怎么能知道,如果我选择的号码是正确的? (1024 * 2)

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

我试图找到空闲的公式:

I'm trying to find the idle formula :

假设每个文件大小为 F MB ,我有研究MB 拉姆和块大小我HD是 B KB - 没有任何公式,我可以建立找到空闲缓冲区的大小?

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 ?

推荐答案

4KB是一个不错的选择。更多信息看这个:
文件IO与数据流 - 最佳的内存缓冲区大小

4KB is a good choice. for more info look to this:
File IO with Streams - Best Memory Buffer Size

问候

这篇关于与文件在C#中工作时,确定缓冲区的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆