Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 389
Yara Avatar asked Jun 06 '12 14:06

Yara


People also ask

Is JSON faster than XML?

JSON is faster because it is designed specifically for data interchange. JSON encoding is terse, which requires less bytes for transit. JSON parsers are less complex, which requires less processing time and memory overhead. XML is slower, because it is designed for a lot more than just data interchange.

Why is JSON more lightweight than XML?

(a) the JSON data model is simpler; it has fewer different kinds of object and they have fewer properties. (c) the serialized syntax of JSON has less redundancy (is less verbose) than the syntax of XML. Of course, these differences are because JSON was designed for a different purpose than XML.

Why would you use XML over JSON?

XML advantages over JSON One of the most significant advantages of XML is that we can put metadata into the tags in the form of attributes. JSON simply lacks this capability. In JSON, attributes will be added as other member fields in data representation that may NOT be desired.


2 Answers

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.

like image 170
Wedge Avatar answered Sep 21 '22 09:09

Wedge


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.

like image 37
Adam Houldsworth Avatar answered Sep 17 '22 09:09

Adam Houldsworth