Performance: use a BinaryReader on a MemoryStream to read a byte array, or read directly?
- by Virtlink
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?