Tag: 二进制读取器

.NET中更快(不安全)的BinaryReader

我遇到了一个情况,我有一个非常大的文件,我需要从中读取二进制数据。 因此,我意识到.NET中的默认BinaryReader实现非常慢。 用.NET Reflector查看它后,我发现了这个: public virtual int ReadInt32() { if (this.m_isMemoryStream) { MemoryStream stream = this.m_stream as MemoryStream; return stream.InternalReadInt32(); } this.FillBuffer(4); return (((this.m_buffer[0] | (this.m_buffer[1] << 8)) | (this.m_buffer[2] << 0x10)) | (this.m_buffer[3] << 0x18)); } 这让我觉得非常低效,想到自32位CPU发明以来计算机是如何设计用于32位值的。 所以我使用这样的代码创建了我自己的(不安全的)FastBinaryReader类: public unsafe class FastBinaryReader :IDisposable { private static byte[] buffer = new byte[50]; //private Stream baseStream; […]