是MemoryStream比FileStream快吗? [英] Is MemoryStream is faster than FileStream ?

查看:81
本文介绍了是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;

推荐答案

如果您查看源代码,我希望ReadAllBytes确实可以完成您的工作.最好使用快捷方式,因为它更整洁,但这就是全部.
I expect if you look into the source, that ReadAllBytes does exactly what you''re doing. It''s better to use the shortcut as it''s neater, but that is all.


我想它取决于文件的大小和可用的内存量.您应该尝试对这两种方法进行概要分析,然后看看哪种方法对您来说更快.

如果文件很大,并且您要容纳的内存量很小,那么将所有字节放入byte [],然后放入内存流中将占用大量的虚拟内存,进而占用大量的虚拟内存.大量页面错误.

OTOH,使用FileStream进行随机访问将再次导致大量磁盘访问.但是操作系统和FileStream类都可以进行缓冲-尽管我不知道在多大程度上.
I guess it depends on the size of the file, and the amount of memory available. You should try profiling both approaches and see which one is faster for you.

If the file size is large and you have a small amount of memory to fit it in, getting all bytes into a byte[] and then into the memory stream is going to take up quite a bit of virtual memory, and in turn, a large number of page faults.

OTOH, using a FileStream to do random access will again lead to large number of disk accesses. But both the operating system and the FileStream class do buffering - although to what extent, I don''t know.


好吧,我选择MS ...这里是结果


使用MS

00:00:00.0014218
00:00:00.0013840
00:00:00.0014123
00:00:00.0016129
00:00:00.0018564
00:00:00.0034196
00:00:00.0015400
00:00:00.0015265
00:00:00.0016011



直接

00:00:00.0093724
00:00:00.0086874
00:00:00.0086332
00:00:00.0087101
00:00:00.0087691
00:00:00.0088588
00:00:00.0090902
00:00:00.0089308
00:00:00.0087806
00:00:00.0085874
00:00:00.0087815
00:00:00.0090007



Well, I go for MS...here are results


with MS

00:00:00.0014218
00:00:00.0013840
00:00:00.0014123
00:00:00.0016129
00:00:00.0018564
00:00:00.0034196
00:00:00.0015400
00:00:00.0015265
00:00:00.0016011



directly

00:00:00.0093724
00:00:00.0086874
00:00:00.0086332
00:00:00.0087101
00:00:00.0087691
00:00:00.0088588
00:00:00.0090902
00:00:00.0089308
00:00:00.0087806
00:00:00.0085874
00:00:00.0087815
00:00:00.0090007



   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快吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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