Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Performance: BinaryFormatter vs. XmlSerializer

I read very often that the BinaryFormatter has better performance then XmlSerializer. Out of curiosity, I wrote a test-app.

a wtf moment... why is Xml so much faster than Bin (especially the deserialization)?

using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Xml.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;

namespace SerPlayground
{
    class Program
    {
        static void Main(string[] args)
        {
            var items = new List<TestClass>();
            for (int i = 0; i < 1E6; i++)
            {
                items.Add(new TestClass() { Name = i.ToString(), Id = i });
            }

            File.Delete("test.bin");
            using (var target = new FileStream("test.bin", FileMode.OpenOrCreate))
            {
                System.Threading.Thread.Sleep(1000);
                var bin = new BinaryFormatter();
                var start = DateTime.Now;
                bin.Serialize(target, items);
                Console.WriteLine("Bin: {0}", (DateTime.Now - start).TotalMilliseconds);

                target.Position = 0;
                System.Threading.Thread.Sleep(1000);
                start = DateTime.Now;
                bin.Deserialize(target);
                Console.WriteLine("Bin-D: {0}", (DateTime.Now - start).TotalMilliseconds);
            }

            File.Delete("test.xml");
            using (var target = new FileStream("test.xml", FileMode.OpenOrCreate))
            {
                System.Threading.Thread.Sleep(1000);
                var xml = new XmlSerializer(typeof(List<TestClass>));
                var start = DateTime.Now;
                xml.Serialize(target, items);
                Console.WriteLine("Xml: {0}", (DateTime.Now - start).TotalMilliseconds);

                target.Position = 0;
                System.Threading.Thread.Sleep(1000);
                start = DateTime.Now;
                xml.Deserialize(target);
                Console.WriteLine("Xml-D: {0}", (DateTime.Now - start).TotalMilliseconds);
           }

            Console.ReadKey();
        }
    }

    [Serializable]
    public class TestClass
    {
        public string Name { get; set; }
        public int Id { get; set; }
    }
}

my results:

Bin: 13472.7706
Bin-D: 121131.9284
Xml: 8917.51
Xml-D: 12841.7345
like image 941
Lukas Avatar asked Feb 03 '23 02:02

Lukas


1 Answers

Because you are serialising an object that doesn't have any properties.

If you serialise something different that actually contains some data, like for example a string, the binary serialiser is a lot faster than the XML serialiser.

I did this change to your code:

items.Add("asfd");

and I get this result:

Xml: 1219.0541
Bin: 165.0002

A part of the difference is of course that the XML file is about ten times larger than the binary file.

like image 112
Guffa Avatar answered Feb 11 '23 08:02

Guffa