简体   繁体   中英

Json and Xml serialization, what is better performance?

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?

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.

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.

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.

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

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.

This is an update to @Wedge's answer from 2014.

It was written in .NET 6.0 and also makes use of the popular Newtonsoft.Json library. I also increased the number of iterations. Previously it was 1, 1,000, and 10,000. This version now also does 100,000, 1,000,000, and 10,000,000 iterations but is still very unscientific and not realistic.

Below is the code used. Be sure to import Newtonsoft.Json into your project if you want to reproduce this.

using Newtonsoft.Json;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Text;

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

// Xml serializer
string xmlConfig = "";
var xmlSerializer = new DataContractSerializer(typeof(Config));
using (var xmlStream = new MemoryStream())
{
    xmlSerializer.WriteObject(xmlStream, myConfig);
    xmlConfig = Encoding.UTF8.GetString(xmlStream.ToArray());
}

// Json serializer
string jsonConfig = "";
var jsonSerializer = new DataContractJsonSerializer(typeof(Config));
using (var jsonStream = new MemoryStream())
{
    jsonSerializer.WriteObject(jsonStream, myConfig);
    jsonConfig = Encoding.UTF8.GetString(jsonStream.ToArray());
}

// Newtonsoft.Json serializer.
string newtonsoftJsonConfig = "";
var newtonsoftJsonSerializer = new JsonSerializer();
using (var newtonSoftMemoryStream = new MemoryStream())
using (var writer = new StreamWriter(newtonSoftMemoryStream))
using (var newtonsoftJsonWriter = new JsonTextWriter(writer))
{
    newtonsoftJsonSerializer.Serialize(newtonsoftJsonWriter, myConfig);
    newtonsoftJsonWriter.Flush();
    newtonSoftMemoryStream.Position = 0;
    newtonsoftJsonConfig = Encoding.UTF8.GetString(newtonSoftMemoryStream.ToArray());
}

// Create a group of 5 different tests.
int[] counterArray = { 1, 1000, 10000, 100000, 1000000, 10000000 };
foreach (var iterations in counterArray)
{
    // Serialize XML.
    var xmlTimer = Stopwatch.StartNew();
    SerializeXML(xmlConfig, iterations);
    xmlTimer.Stop();

    // Serialize JSON.
    var jsonTimer = Stopwatch.StartNew();
    SerializeJSON(jsonConfig, iterations);
    jsonTimer.Stop();

    // Serialize JSON (Newtonsoft).
    var newtonsoftJsonTimer = Stopwatch.StartNew();
    SerializeNewtonsoftJson(newtonsoftJsonConfig, iterations);
    newtonsoftJsonTimer.Stop();

    Console.WriteLine($"XML Serialization {iterations}: {xmlTimer.Elapsed.TotalMilliseconds}ms");
    Console.WriteLine($"JSON Serialization {iterations}: {jsonTimer.Elapsed.TotalMilliseconds}ms");
    Console.WriteLine($"Newtonsoft.Json Serialization {iterations}: {newtonsoftJsonTimer.Elapsed.TotalMilliseconds}ms");
    Console.WriteLine();
}

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

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

static void SerializeNewtonsoftJson(string json, int iterations)
{
    // Newtonsoft.Json serializer.
    var newtonsoftJsonSerializer = new JsonSerializer();
    for (var i = 0; i < iterations; i++)
    {
        using var stream = new MemoryStream(Encoding.UTF8.GetBytes(json));
        using var reader = new JsonTextReader(new StreamReader(stream, new UTF8Encoding()));
        var serialized = newtonsoftJsonSerializer.Deserialize<Config>(reader);
    }
}

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

And now the benchmark results:

XML Serialization 1: 4.3958ms
JSON Serialization 1: 3.3516ms
Newtonsoft.Json Serialization 1: 37.5018ms

XML Serialization 1000: 11.137ms
JSON Serialization 1000: 6.8425ms
Newtonsoft.Json Serialization 1000: 2.4205ms

XML Serialization 10000: 39.1409ms
JSON Serialization 10000: 56.8301ms
Newtonsoft.Json Serialization 10000: 21.352ms

XML Serialization 100000: 358.903ms
JSON Serialization 100000: 519.5981ms
Newtonsoft.Json Serialization 100000: 197.7238ms

XML Serialization 1000000: 3585.8248ms
JSON Serialization 1000000: 5256.336ms
Newtonsoft.Json Serialization 1000000: 2006.7546ms

XML Serialization 10000000: 36326.6082ms
JSON Serialization 10000000: 53260.1445ms
Newtonsoft.Json Serialization 10000000: 20506.9946ms

As you can see, doing a single serialization is extremely slow in Newtonsoft.Json - nearly 10 times slower. However , over multiple iterations, this number drops significantly to the point that Newtonsoft.Json is a clear winner.

Again, this is not scientific, and is not realistic, but does give some idea of the performance of serialization using Newtonsoft.Json.

If anyone would like to point out problems with my answer, offer suggestions to improve performance on any of them, please let me know.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM