C#应用MemoryStream提高File读取速度

一、场景:

需要将有一定格式的File里的内容读取到已经定义的类中,譬如一个二进制文件里的内容读取到一个新的DataStructure里面。

1. File不是很大,一次将所有内容Load到内存中,不会占用太多Memory;

2. 二进制文件无法直接反序列化成一个Object,需要一个映射才能完成转换.

二、为什么不用FileStream:

首先,我们来看一下FileStream的代码以及原理:

FileStream fs = new FileStream(binaryFilePath, FileMode.Open, FileAccess.Read);

Encoding enc = Encoding.ASCII;
using (BinaryReader br = new BinaryReader(fs,enc))
{

//

}

BinaryReader 提供了ReadInt16,ReadChar,ReadDouble,……但是每次Position.Seek都会去定位流的指针,所以会耗费一些时间。

但是如果我们一次性的将所有内容都独到内存(Memory)中,然后操作Memory是不是更快呢?

三、MemoryStream实现方法

FileStream fs = new FileStream(binaryFilePath, FileMode.Open, FileAccess.Read);
//Read all bytes into an array from the specified file.
int nBytes = (int)fs.Length;//计算流的长度
byte[] byteArray = new byte[nBytes];//初始化用于MemoryStream的Buffer
int nBytesRead = fs.Read(byteArray, 0, nBytes);//将File里的内容一次性的全部读到byteArray中去
using (MemoryStream br = new MemoryStream(byteArray))//初始化MemoryStream,并将Buffer指向FileStream的读取结果数组
{
/your code

}

四、性能分析

分别用以上两种方法Load File:1,2,3,4,5,6,将其Load的时间记录下来:

FileFile Size(Mb)FileStream(ms)MemoryStream(ms)Performance Enhancement
10.59213922.35
27.1445414663.04
314784833662.33
4281602562422.57
5442119692682.26
67327533145031.90

 

可以看出:应用了MemoryStream性能提高了2-3倍。

 

可以看出:随着File内容的增加,性能提高的就越来越缓慢,因为他占用了更多的内存空间,可以说是:“以空间换时间”。

转自:http://www.deepleo.com/archives/680

转载于:https://www.cnblogs.com/deepleo/archive/2012/02/06/2340017.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值