Performance comparison of most popular serializers

Damir Dobric Posts

Next talks:

 

    

Follow me on Twitter: #ddobric



 

 

Archives

This post shows the performance differences between most popular .NET serializes. Results shown in the table below are related to following serializers:

  • XmlSerializer
  • DataContractSerializer with XmlDictionaryWriter
  • DataContractSerializer with BinaryWriter
  • Serialization with Binary formatter

    Following table shows all relevant times instrumented by performance tests in Visual Studio.

  

Number of
calls

Elapsed
Exclusive
Time

Application
Exclusive
Time

Elapsed
Inclusive
Time

Application
Inclusive
Time

Serialization

  

  

  

  

  

SerializeXml(object)

10000

9.839409

8.841528

10558.80443

289.755953

SerializeDataContractBin(object)

10000

10.64141

9.719452

8315.242766

225.435274

SerializeDataContractXml(object)

10000

11.19088

9.920973

8058.579323

232.021821

SerializeBin(object)

10000

7.55754

7.246965

7806.438527

9.587866

Derialization

  

  

  

  

  

DeserializeXml()

10000

11.44839

9.050696

3315.922901

262.74053

DerializeDataContractXml()

10000

13.6174

12.388385

2265.471489

327.973494

DerializeDataContractBin()

10000

12.39571

11.214176

2140.909216

213.323018

DeserializeBin()

10000

7.31513

7.096129

2026.720576

8.941365

Following diagram compares the elapsed inclusive time:



Conclusion:

  1. Deserialization of some value is in general faster than serialization of the same value.
  2. The slowest serializer is XmlSerializer.
  3. DataContract serialization is faster than XmlSerializer, but slower than binary serialization.
  4. The binary and xml serialization within DataContractSerializer have approximately same performance.
  5. DataContract serialization is little slower than binary serialization.

Following code snippet shows which methods have been tested. Each of methods has been invoked 10000 times.

        public static void SerializeXml(object value)

        {

            using (FileStream fs = 
            new FileStream("XmlSerializer.xml", FileMode.Create))

            {

                XmlSerializer serializer = new XmlSerializer(value.GetType());

                serializer.Serialize(fs, value);

            }

        }

 

        public static T DeserializeXml<T>()

        {

            using (FileStream fs =
            new FileStream("XmlSerializer.xml", FileMode.Open))

            {

                XmlSerializer serializer = new XmlSerializer(typeof(T));

                object deserializedValue = serializer.Deserialize(fs);

                return (T)deserializedValue;

            }

        }

 

        internal static void SerializeDataContractXml(object value)

        {

            using (FileStream fs = 
            new FileStream("DataFileDContract.xml", FileMode.Create))

            using (XmlDictionaryWriter writer =

                XmlDictionaryWriter.CreateTextWriter(fs))

            {

                DataContractSerializer serializer =

                    new DataContractSerializer(value.GetType());

                serializer.WriteObject(writer, value);

            }

        }

 

        internal static T DerializeDataContractXml<T>()

        {

            T value;

            using (FileStream fs = new 
            FileStream("DataFileDContract.xml", FileMode.Open))

            using (XmlDictionaryReader reader =

                XmlDictionaryReader.CreateDictionaryReader(XmlReader.Create(fs)))

            {

                DataContractSerializer deserializer =

                    new DataContractSerializer(typeof(T));

                value = (T)deserializer.ReadObject(reader);

            }

 

            return value;

        }

 

 

        internal static void SerializeDataContractBin(object value)

        {

            using (FileStream fs = new FileStream("DataFileDContractBin.xml", FileMode.Create))

            using (XmlDictionaryWriter writer =

                XmlDictionaryWriter.CreateBinaryWriter(fs))

            {

                DataContractSerializer serializer =

                    new DataContractSerializer(value.GetType());

                serializer.WriteObject(writer, value);

            }

        }

 

        internal static T DerializeDataContractBin<T>()

        {

            T value;

            using (FileStream fs = new FileStream("DataFileDContractBin.xml", FileMode.Open))

            using (XmlDictionaryReader reader =

                XmlDictionaryReader.CreateBinaryReader(fs, XmlDictionaryReaderQuotas.Max))

            {

                DataContractSerializer deserializer =

                    new DataContractSerializer(typeof(T));

                value = (T)deserializer.ReadObject(reader);

            }

 

            return value;

        }

 

        internal static void SerializeBin(object item)

        {

           FileStream fs = new FileStream("DataFileBin.txt", FileMode.Create);

 

            BinaryFormatter formatter = new BinaryFormatter();

            try

            {

                formatter.Serialize(fs, item);

            }

            catch (SerializationException e)

            {

                Console.WriteLine("Failed to serialize. Reason: " + e.Message);

                throw;

            }

            finally

            {

                fs.Close();

            }

        }

 

 

        internal static T DeserializeBin<T>()

        {

            T value;

 

            FileStream fs = new FileStream("DataFileBin.txt", FileMode.Open);

            try

            {

                BinaryFormatter formatter = new BinaryFormatter();

 

                value = (T)formatter.Deserialize(fs);

 

                return value;

            }

            catch (SerializationException e)

            {

                Console.WriteLine("Failed to deserialize.Reason: "
                + e.Message);

                throw;

            }

            finally

            {

                fs.Close();

            }

        }


Posted Aug 05 2007, 02:33 AM by Damir Dobric
Filed under:

Comments

Damir Dobric Posts wrote Detaching Entities in Linq2Sql
on 01-20-2008 20:28

In two-tier applications, a single DataContext is used for queries and updates. Some of these examples

.NET worker » Blog Archive » XML-Serialisierung von Objekten mit Hilfe von WCF-Klassen wrote .NET worker &raquo; Blog Archive &raquo; XML-Serialisierung von Objekten mit Hilfe von WCF-Klassen
on 03-26-2008 10:59

Pingback from  .NET worker  &raquo; Blog Archive   &raquo; XML-Serialisierung von Objekten mit Hilfe von WCF-Klassen

Damir Dobric wrote re: Performance comparison of most popular serializers
on 03-26-2008 11:35
Waqas wrote re: Performance comparison of most popular serializers
on 05-30-2008 7:55

It will be more interesting to compare the performance of XMLSerializer by generating the serialization assembly for XML Serializer.

Andrei Rinea wrote re: Performance comparison of most popular serializers
on 05-06-2009 14:35

I'm surprised to see such a small performance difference between the XML serialization and the binary one..

chatwinbia wrote re: Performance comparison of most popular serializers
on 11-18-2009 10:37

features doi medium vapor source sensitivity store simulation

developers.de is a .Net Community Blog powered by daenet GmbH.