本文介绍了是MemoryStream比FileStream快吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在读取一个二进制文件,并且需要太多的指针移动.我知道MS比FS快得多.但是我正在做的是,首先读取所有字节以进行缓存,然后使用MS.那有什么不同吗?还是我应该直接阅读文件?
像这样的东西
I''m reading a binary file and need too many pointer movements. And I know MS is much faster than FS. But what I''m doing is that, first I read all bytes to cache then use MS. So will it make any difference or I should directly read the file ?
something like this
byte[] data = File.ReadAllBytes(path);
MemoryStream ms = new MemoryStream(data);
BinaryReader br = new BinaryReader(ms);
或
or
BinaryReader br = new BinaryReader(File.OpenRead(path));
创建BinaryReader之后,我必须做数千个指针移动,例如
Edited :
after creating BinaryReader I have to do thousands of pointers movements like
br.BaseStream.Position = 99999;
br.BaseStream.Position = 2;
br.BaseStream.Position = 86765;
推荐答案
System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
sw.Start();
string path = @"D:\american.gxt";
byte[] data = File.ReadAllBytes(path);
MemoryStream ms = new MemoryStream(data);
BinaryReader br = new BinaryReader(ms);
// BinaryReader br = new BinaryReader(File.OpenRead(path)); //uncomment this line for II method
Random r = new Random();
int length = (int)br.BaseStream.Length;
for (int a = 0; a < 10000; a++)
{
br.BaseStream.Position = r.Next(0, length);
}
sw.Stop();
richTextBox1.AppendText(sw.Elapsed.ToString()+"\r\n");
这篇关于是MemoryStream比FileStream快吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!