C#学习教程:.NET中更快(不安全)的BinaryReader因此,我意识到.NET中的默认BinaryReader实现非常慢。使用.NETReflector查看后,我发现:publicvirtualintReadInt32(){if(this.m_isMemoryStream){返回流.InternalReadInt32();}this.FillBuffer(4);返回(((this.m_buffer[0]|(this.m_buffer[1]<<8))|(this.m_buffer[2]<<0x10))|(this.m_buffer[3]<<0x18));这让我觉得效率很低,想想自从32位CPU发明以来,计算机是如何为32位值设计的。所以我用这样的代码创建了自己的(不安全的)FastBinaryReader类:publicunsafeclassFastBinaryReader:IDisposable{privatestaticbyte[]buffer=newbyte[50];//私有流baseStream;公共流BaseStream{得到;私有集;}publicFastBinaryReader(Streaminput){BaseStream=input;}publicintReadInt32(){BaseStream.Read(buffer,0,4);fixed(byte*numRef=&(buffer[0])){return*(((int*)numRef));哪个更快-我设法将读取500MB文件所需的时间减少了5-7秒,但总体上仍然很慢(最初是29秒,现在使用我的FastBinaryReader是22秒)。对于为什么读取这么小的文件仍然需要这么长时间,我还是有点困惑。如果我将文件从一个磁盘复制到另一个磁盘,只需几秒钟,因此磁盘吞吐量不是问题。我进一步内联了ReadInt32之类的调用,最后得到了这段代码:.BaseStream.Position
