Performance: use a BinaryReader on a MemoryStream to read a byte array, or read directly?

Posted by Virtlink on Stack Overflow See other posts from Stack Overflow or by Virtlink
Published on 2010-06-12T15:37:45Z Indexed on 2010/06/12 15:42 UTC
Read the original article Hit count: 590

I would like to know whether using a BinaryReader on a MemoryStream created from a byte array (byte[]) would reduce performance significantly.

There is binary data I want to read, and I get that data as an array of bytes. I am currently deciding between two approaches to read the data, and have to implement many reading methods accordingly. After each reading action, I need the position right after the read data, and therefor I am considering using a BinaryReader. The first, non-BinaryReader approach:

object Read(byte[] data, ref int offset);

The second approach:

object Read(BinaryReader reader);

Such Read() methods will be called very often, in succession on the same data until all data has been read.

So, using a BinaryReader feels more natural, but has it much impact on the performance?

© Stack Overflow or respective owner

Related posts about c#

Related posts about Performance