[Solved] Speed up reading a file to multiple bytes array and copy the offsets by if statement


You might have fallen in the “I use bytes and arrays so I’m low level so the code will be fast” trap. C# and .NET provide its users with efficient abstractions to avoid this, in this case probably a BinaryReader and its ReadString method.

You code has problems:

  • You don’t read the entire file (you stop at the last buffer of 1048576 bytes filled up, the next x bytes wont be read)
  • You read a byte array then convert it to string ? you should use the .NET framework IO API (Streams) and allocate strings directly
  • You should encapsulate IO operations on files in a ‘using’ statement to avoid resources leaks (on file handles).

Performance issues I see:

  • Allocation of a big bytes array for nothing
  • Convert calls that can be avoided

Code sample (I did not compile it but it gives you the idea of a clean and efficient file reading, see my MSDN links for more examples)

string myText = string.Empty;
using(var reader = new BinaryReader(openFileDialog1.FileName))
{
  string line;
  while((line = reader.ReadString()) != null)
  {
    myText += YouLogic(line); // use a StringBuilder if the number of string concatenations is high
  }
}

As a side note, for very big files, please consider using C# Memory Mapped Files

EDIT:
BinaryReader contains Read methods for all the needed data types, it should allow you to efficiently read any type of binary file.

As for you additional question, look at ReadBytes from MSDN:

//Offset 100 bytes:
var offsetRead = reader.ReadBytes(100);
// read one byte
byte readByte = reader.ReadByte();
// compare it to the expected value
if(readByte == myByte)
{
   // copy the offset read
   var myArray = new byte[100];
   Array.Copy(offsetRead, myArray, offsetRead.Length);
   DoSomething(myArray);
}

3

solved Speed up reading a file to multiple bytes array and copy the offsets by if statement