1

I've used BinaryFormatter in order to serialize/deserialize objects to a byte array. But it's too slow. Here's my code:

IFormatter formatter = new BinaryFormatter();
MemoryStream stream = new MemoryStream();
formatter.Serialize(stream, this);
stream.Close();
byte[] currentByteArray = stream.ToArray();

Is it possible to improve that code, in order to speed it up. Or what is my alternatives? I've seen several other serializatiors like xmlserialization but I don't want to write it to file, just as a byte array.

Thanks in advance!

random
  • 487
  • 1
  • 9
  • 19
  • Please include the class code and the metrics in your question. – Oguz Ozgul Nov 19 '15 at 08:18
  • also, how big is teh serialized data? what size are we talking about here? – lordkain Nov 19 '15 at 08:19
  • 2
    `stream.Close();` should be *after* `byte[] currentByteArray = stream.ToArray();`, not *before* – Dmitry Bychenko Nov 19 '15 at 08:31
  • Are you serializing dataobjects? like for communication? – Jeroen van Langen Nov 19 '15 at 08:44
  • @DmitryBychenko No. It should be in a `finally` block after the `.ToArray()`. In fact, stream should just be in a `using` block. – Aron Nov 19 '15 at 09:28
  • The data is serialized down to 10MB, it takes about 200ms. Yes it's data objects used in communication. – random Nov 19 '15 at 09:48
  • Suggest you try [running a profiler](http://stackoverflow.com/questions/3927/what-are-some-good-net-profilers) and seeing what it tells you. To get started check [here](https://msdn.microsoft.com/en-us/library/ms182372.aspx). There's not enough information in your question for us to see if you're doing anything specifically bad with `BinaryFormatter`. See [Fastest way to serialize and deserialize .NET object](http://stackoverflow.com/questions/4143421) for comparisons between serializers. – dbc Nov 20 '15 at 02:42
  • An [alternative serializer](https://dotnetfiddle.net/T7BUyB) (disclaimer: written by me) can be a solution. But please note that using binary serializers have [security implications](https://stackoverflow.com/a/67019995/5114784) as well. – György Kőszeg Apr 09 '21 at 12:10

1 Answers1

3

Your code can be improved if you place disposing in finally statement like guys said in comments:

IFormatter formatter;
MemoryStream stream;
try
{
    formatter = new BinaryFormatter();
    stream = new MemoryStream();
    formatter.Serialize(stream, this);
    byte[] currentByteArray = stream.ToArray();
}
finally
{
   if(stream!=null)
      stream.Close();
}

However, the above code does not improve performance of BinaryFormatter class cause it works and is used correctly. But you can use other libraries.

One of the fastest and general purpose serializer in .NET is Protobuf-net. For example:

[ProtoContract]
class SubMessageRepresentations
{
   [ProtoMember(5, DataFormat = DataFormat.Default)] 
   public SubObject lengthPrefixedObject;
   [ProtoMember(6, DataFormat = DataFormat.Group)]
   public SubObject groupObject;
}

[ProtoContract(ImplicitFields=ImplicitFields.AllFields)]
class SubObject { public int x; }


using (var stream = new MemoryStream()) {
  _pbModel.Serialize(
   stream, new SubMessageRepresentations {
        lengthPrefixedObject = new SubObject { x = 0x22 },
        groupObject = new SubObject { x = 0x44 }
   });
byte[] buf = stream.GetBuffer();
for (int i = 0; i < stream.Length; i++)
Console.Write("{0:X2} ", buf[i]);
}
openshac
  • 4,966
  • 5
  • 46
  • 77
StepUp
  • 36,391
  • 15
  • 88
  • 148