-->

Json and Xml serialization, what is better perform

2020-08-10 09:39发布

问题:

I have to store some config info in file. In C# code config data represents by class and in file I am going to save this class in json or xml format. So, what is the best performance of serialization json or xml?

回答1:

Well instead of guessing, I have the answer. Here is the test program:

class Program
{
    static void Main(string[] args)
    {
        string xmlConfig = "";
        string jsonConfig = "";

        Config myConfig = new Config()
        {
            value = "My String Value",
            DateStamp = DateTime.Today,
            counter = 42,
            Id = Guid.NewGuid()
        };

        // Make both strings
        DataContractSerializer xmlSerializer = new DataContractSerializer(typeof(Config));
        using (MemoryStream xmlStream = new MemoryStream())
        {
            xmlSerializer.WriteObject(xmlStream, myConfig);
            xmlConfig = Encoding.UTF8.GetString(xmlStream.ToArray());
        }

        DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(Config));
        using (MemoryStream jsonStream = new MemoryStream())
        {
            jsonSerializer.WriteObject(jsonStream, myConfig);
            jsonConfig = Encoding.UTF8.GetString(jsonStream.ToArray());
        }

        // Test Single
        var XmlSingleTimer = Stopwatch.StartNew();
        SerializeXML(xmlConfig, 1);
        XmlSingleTimer.Stop();

        var JsonSingleTimer = Stopwatch.StartNew();
        SerializeJSON(jsonConfig, 1);
        JsonSingleTimer.Stop();

        // Test 1000
        var XmlTimer = Stopwatch.StartNew();
        SerializeXML(xmlConfig, 1000);
        XmlTimer.Stop();

        var JsonTimer = Stopwatch.StartNew();
        SerializeJSON(jsonConfig, 1000);
        JsonTimer.Stop();

        // Test 10000
        var XmlTimer2 = Stopwatch.StartNew();
        SerializeXML(xmlConfig, 10000);
        XmlTimer2.Stop();

        var JsonTimer2 = Stopwatch.StartNew();
            SerializeJSON(jsonConfig, 10000);
        JsonTimer2.Stop();

        Console.WriteLine(String.Format("XML Serialization Single: {0}ms", XmlSingleTimer.Elapsed.TotalMilliseconds));
        Console.WriteLine(String.Format("JSON Serialization Single: {0}ms", JsonSingleTimer.Elapsed.TotalMilliseconds));
        Console.WriteLine();
        Console.WriteLine(String.Format("XML Serialization 1000: {0}ms", XmlTimer.Elapsed.TotalMilliseconds));
        Console.WriteLine(String.Format("JSON Serialization 1000: {0}ms ", JsonTimer.Elapsed.TotalMilliseconds));
        Console.WriteLine();
        Console.WriteLine(String.Format("XML Serialization 10000: {0}ms ", XmlTimer2.ElapsedMilliseconds));
        Console.WriteLine(String.Format("JSON Serialization 10000: {0}ms ", JsonTimer2.ElapsedMilliseconds));
    }

    public static void SerializeXML(string xml, int iterations)
    {
        DataContractSerializer xmlSerializer = new DataContractSerializer(typeof(Config));
        for (int i = 0; i < iterations; i++)
        {
            using (MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(xml)))
            {
                Config serialized = (Config)xmlSerializer.ReadObject(stream);
            }
        }
    }

    public static void SerializeJSON(string json, int iterations)
    {
        DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(Config));
        for (int i = 0; i < iterations; i++)
        {
            using (MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(json)))
            {
                Config serialized = (Config)jsonSerializer.ReadObject(stream);
            }
        }
    }
}

public class Config
{
    public string value;
    public DateTime DateStamp;
    public int counter;
    public Guid Id;
}

And this is the measured output:

XML Serialization Single: 2.3764ms
JSON Serialization Single: 2.1432ms

XML Serialization 1000: 13.7754ms
JSON Serialization 1000: 13.747ms

XML Serialization 10000: 100ms
JSON Serialization 10000: 134ms

JSON consistently came out just a tiny bit faster after 1 iteration. After 1000 iterations there really was no difference. After 10000 iterations, XML was clearly faster.

At this point I can't explain why JSON would be faster one at a time, but XML would be faster when it is repeated. Possibly due to caching or something fancy in the library. You can see that the JsonSerializer scaled linearly, increasing the iterations by an order of 10 linearly increased the elapsed time by an order of 10. The XmlSerializer behaved differently though, its performance did not scale in a linear way.

I repeated this several times and consistently got the same results.

So, the lesson is if you are just parsing a single object one time, then JSON will be slightly better. But if you are repeatedly parsing objects, then XML might perform better. Although, I haven't tested what would happen if the object values change with each iteration, that might make a difference.

Also note, I am using the native Runtime.Serialization library here. Other libraries will likely produce different results.

Edit: I just tried this while generating a new Guid and random Int every time the strings are called. It made no difference to the single or 10000 iteration tests. But for 1000 iterations, JSON was about 1ms faster. So it seems like the XML serializer really is caching the values.



回答2:

When I go looking for configuration in a .NET application, I expect to find an XML file somewhere called MyApp.exe.config.

Sticking with the principle of least surprise I would favour XML serialization over JSON. There is an added benefit that XML formatted configuration can be adapted to work with the Configuration API. Both otherwise have the same sort of support: platform agnostic, decent parsers, text-based, etc.

Performance is only an issue when it becomes an issue. I am a fan of identifying potential issues before I code them, but that is usually on performance issues introduced by architectural decisions. Something like this, small and fairly self-contained, won't be difficult to change if it proves to be a problem under profiling.



回答3:

Json can be somethimes less readable by humans than xml, but the size of the file generated by json is smaller. So if you need to send the file over network, Json may be the better choise, or if you want to be able to read it, XML is better. Another good thing, is that in .NET 4 you have the dynamic keyword, and you can convert your Json directly to a C# object.



回答4:

in my opinion all is depend what you need to do and how to you want implement, here is a good article comparing JSON and XML. compression and deserialization in client side I choose JSON.

Good Luck.

http://dotnet.dzone.com/articles/json-vs-xml-net-developer%E2%80%99s



回答5:

The cost to serialize would be roughly the same. It's unlikely to be a noticeable difference. Use the format that your users will feel most comfortable modifying (since it's a config file).

The real performance difference might happen when you need to send the JSON or XML across a network. Then, the performance depends on how much stuff you're sending, and since JSON is usually more concise than XML, it will generally perform better over a network.