假设我正在写几个文件到磁盘,介于2MB和5GB之间.什么是FileStream的合理缓冲区值?
使用几兆字节的缓冲区是否合理,还是应该坚持使用千字节缓冲区?
我想在图像元数据块的中间添加一些字符串.根据某些特定标记.我必须在字节级别上执行它,因为.NET不支持自定义元数据字段.
构建块就像1C 02 XX YY YY ZZ ZZ ZZ ...XX是我需要追加的字段的ID,YY YY是它的大小,ZZ =数据.
我想应该或多或少地读取所有图像数据直到这个标记(1C 02 XX)然后增加大小字节(YY YY),在ZZ的末尾添加数据然后添加原始文件的其余部分?它是否正确?
我该怎么做呢?它需要使用4-5 MB JPEG文件尽可能快地工作.
我有一个问题,我找不到理由.我正在创建一个自定义存档文件.我MemoryStream用来存储数据,最后我用a FileStream来将数据写入磁盘.
我的硬盘是SSD,但速度太慢了.当我尝试只向文件写入95 MB时,写入需要12秒!
我尝试了Filestream.Write,File.WriteAllBytes但它是一样的.
最后,我有了一个想法,复制它,它快了100倍!
我需要知道为什么会发生这种情况以及写入函数出了什么问题.
这是我的代码:
//// First of all I create an example 150MB file
Random randomgen = new Random();
byte[] new_byte_array = new byte[150000000];
randomgen.NextBytes(new_byte_array);
//// I turned the byte array into a MemoryStream
MemoryStream file1 = new MemoryStream(new_byte_array);
//// HERE I DO SOME THINGS WITH THE MEMORYSTREAM
/// Method 1 : File.WriteAllBytes | 13,944 ms
byte[] output = file1.ToArray();
File.WriteAllBytes("output.test", output);
// Method 2 …Run Code Online (Sandbox Code Playgroud) 我正在阅读二进制文件,这是一个示例:
public static byte[] ReadFully(Stream input)
{
byte[] buffer = new byte[16*1024];
int read;
while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
{
......
}
}
Run Code Online (Sandbox Code Playgroud)
显然缓冲区大小(16*1024)在性能方面有很大的作用.我读过它取决于I/O技术(SATA,SSD,SCSI)等)以及它上面存在文件的分区的片段大小(我们可以在格式化分区期间定义).
但问题是: 是否有任何公式或最佳实践来定义缓冲区大小?现在,我正在根据反复试验来定义.
编辑: 我已经在我的服务器上测试了不同缓冲区大小的应用程序,并且我获得了4095*256*16(16 MB)的最佳性能!4096慢了4秒.
以下是一些非常有用的旧帖子,但我仍然无法得到原因:
我经常看到4096用作整个地方的默认缓冲区大小.是否有任何理由选择4096而不是另一个值?
我有这个简单的代码,它将文本文件合并到一个文本文件中:
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);
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题是关于chunkSize大小.
我怎么知道我选择的号码是否合适?(1024*2)
我正试图找到空闲的公式:
假设每个文件大小都是F mb,而且我有R mbRam和我的Hd的块大小B kb- 是否有任何公式我可以构建以找到空闲缓冲区大小?
我现在有点没钱了...
尝试1
在Powershell中使用iwr。它可以工作,显示进度,但速度慢10倍,直到文件在内存中才刷新:(。
powershell -command "& { iwr https://github.com/mitchellspryn/AirsimHighPolySuv/releases/download/V1.0.0/SUV.zip -OutFile SUV.zip }"
Run Code Online (Sandbox Code Playgroud)
尝试2
在Powershell中使用.Net webclient。它有效,但是没有任何进展,您无法通过Ctrl + C :(。终止。
powershell -command "& { (New-Object System.Net.WebClient).DownloadFile('https://github.com/mitchellspryn/AirsimHighPolySuv/releases/download/V1.0.0/SUV.zip', 'SUV.zip') }"
Run Code Online (Sandbox Code Playgroud)
尝试3
在Powershell中使用BITS传输。它可以工作,显示进度并且几乎完美……直到您发现它神秘地在GitHub上不起作用(错误禁止403)!
powershell -command "& { Start-BitsTransfer -Source https://github.com/mitchellspryn/AirsimHighPolySuv/releases/download/V1.0.0/SUV.zip -Destination SUV.zip }"
Run Code Online (Sandbox Code Playgroud) 我已经去了FTP上传功能,但有一些我想问的问题是缓冲区大小,我把它设置为20KB是什么意思,如果我增加/减少它会有什么不同吗?
private void Upload(string filename)
{
FileInfo fi = new FileInfo(filename);
FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create("ftp://" + textBox1.Text + "/" + Path.GetFileName(filename));
ftp.Credentials = new NetworkCredential(textBox2.Text, textBox3.Text);
ftp.Method = WebRequestMethods.Ftp.UploadFile;
ftp.UseBinary = true;
ftp.KeepAlive = false;
ftp.ContentLength = fi.Length;
// The buffer size is set to 20kb
int buffLength = 20480;
byte[] buff = new byte[buffLength];
int contentLen;
//int totalReadBytesCount = 0;
FileStream fs = fi.OpenRead();
try
{
// Stream to which the file to be upload is written
Stream strm …Run Code Online (Sandbox Code Playgroud) 我看到了很多CopyStream实现的例子,但是当我们复制流时,我对缓冲区大小有疑问.
CopyStreams实现之一的示例:
private void ReadWriteStream(Stream readStream, Stream writeStream)
{
int Length = 256;
Byte[] buffer = new Byte[Length];
int bytesRead = readStream.Read(buffer, 0, Length);
// write the required bytes
while (bytesRead > 0)
{
writeStream.Write(buffer, 0, bytesRead);
bytesRead = readStream.Read(buffer, 0, Length);
}
readStream.Close();
writeStream.Close();
}
Run Code Online (Sandbox Code Playgroud)
问题是:
相关问题:
带有Streams的文件IO - 最佳内存缓冲区大小 - 很好的文件IO答案.但是在内存复制方面呢?
我的情况:
有MemotyStream,我创建使用ClosedXML workbook.SaveAs(memoryStream);和它分配在托管堆中的内存量巨大.我查看了源代码,发现有一个使用8*1024缓冲区大小的CopyStream方法.可以改变这个大小以某种方式减少内存使用?
注意: Stream占用大约1Gb的内存.
c# ×8
buffer ×4
filestream ×3
.net ×2
io ×2
bytearray ×1
c#-4.0 ×1
command-line ×1
filesystems ×1
ftp ×1
iptc ×1
memorystream ×1
optimization ×1
performance ×1
powershell ×1
size ×1
stream ×1
streamwriter ×1
upload ×1
windows ×1