title: protobuf-net NOT faster than binary serialization?
body: <p>I wrote a program to serialize a 'Person' class using XMLSerializer, BinaryFormatter and ProtoBuf. I thought protobuf-net should be faster than the other two. Protobuf serialization was faster than XMLSerialization but much slower than the binary serialization. Is my understanding incorrect? Please make me understand this. Thank you for the help.</p>
  
  <p><strong>EDIT :- I changed the code (updated below) to measure the time only for the serialization and not creating the streams and still see the difference. Could one tell me why?</strong></p>
  
  <p><strong>Following is the output</strong>:-</p>
  
  <p>Person got created using protocol buffer in 347 milliseconds</p>
  
  <p>Person got created using XML in 1462 milliseconds</p>
  
  <p>Person got created using binary in 2 milliseconds</p>
  
  <p><strong>Code below</strong></p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using ProtoBuf;
  using System.IO;
  using System.Diagnostics;
  using System.Runtime.Serialization.Formatters.Binary;
  namespace ProtocolBuffers
  {
      class Program
      {
          static void Main(string[] args)
          {
  
              string folderPath  = @"E:\Ashish\Research\VS Solutions\ProtocolBuffers\ProtocolBuffer1\bin\Debug";
              string XMLSerializedFileName = Path.Combine(folderPath,"PersonXMLSerialized.xml");
              string ProtocolBufferFileName = Path.Combine(folderPath,"PersonProtocalBuffer.bin");
              string BinarySerializedFileName = Path.Combine(folderPath,"PersonBinary.bin");
  
              if (File.Exists(XMLSerializedFileName))
              {
                  File.Delete(XMLSerializedFileName);
                  Console.WriteLine(XMLSerializedFileName + " deleted");
              }
              if (File.Exists(ProtocolBufferFileName))
              {
                  File.Delete(ProtocolBufferFileName);
                  Console.WriteLine(ProtocolBufferFileName + " deleted");
              }
              if (File.Exists(BinarySerializedFileName))
              {
                  File.Delete(BinarySerializedFileName);
                  Console.WriteLine(BinarySerializedFileName + " deleted");
              }
  
              var person = new Person
              {
                  Id = 12345,
                  Name = "Fred",
                  Address = new Address
                  {
                      Line1 = "Flat 1",
                      Line2 = "The Meadows"
                  }
              };
  
              Stopwatch watch = Stopwatch.StartNew();
  
              using (var file = File.Create(ProtocolBufferFileName))
              {
                  watch.Start();
                  Serializer.Serialize(file, person);
                  watch.Stop();
              }
  
              //Console.WriteLine(watch.ElapsedMilliseconds.ToString());
              Console.WriteLine("Person got created using protocol buffer in " + watch.ElapsedMilliseconds.ToString() + " milliseconds ");
  
              watch.Reset();
  
              System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(person.GetType());
              using (TextWriter w = new StreamWriter(XMLSerializedFileName))
              {
                  watch.Start();
                  x.Serialize(w, person);
                  watch.Stop();
              }
  
              //Console.WriteLine(watch.ElapsedMilliseconds.ToString());
              Console.WriteLine("Person got created using XML in " + watch.ElapsedMilliseconds.ToString() + " milliseconds");
  
              watch.Reset();
  
              using (Stream stream = File.Open(BinarySerializedFileName, FileMode.Create))
              {
                  BinaryFormatter bformatter = new BinaryFormatter();
                  //Console.WriteLine("Writing Employee Information");
                  watch.Start();
                  bformatter.Serialize(stream, person);
                  watch.Stop();
              }
  
              //Console.WriteLine(watch.ElapsedMilliseconds.ToString());
              Console.WriteLine("Person got created using binary in " + watch.ElapsedMilliseconds.ToString() + " milliseconds");
  
              Console.ReadLine();
  
  
  
          }
      }
  
  
      [ProtoContract]
      [Serializable]
      public class Person
      {
          [ProtoMember(1)]
          public int Id { get; set; }
          [ProtoMember(2)]
          public string Name { get; set; }
          [ProtoMember(3)]
          public Address Address { get; set; }
      }
      [ProtoContract]
      [Serializable]
      public class Address
      {
          [ProtoMember(1)]
          public string Line1 { get; set; }
          [ProtoMember(2)]
          public string Line2 { get; set; }
      }
  }
  </code></pre>