Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Runtime.CompilerServices;
- using System.Text;
- using UnityEngine;
- using LitJson;
- using Newtonsoft.Json;
- using Tiny;
- using Sirenix.Serialization;
- [Serializable]
- public class EmptyClass
- {
- }
- [Serializable]
- public class StringOnly
- {
- public string String;
- }
- [Serializable]
- public class ArrayOnly
- {
- public int[] IntArray;
- }
- [Serializable]
- public class A
- {
- public B B;
- }
- [Serializable]
- public class B
- {
- public C C;
- }
- [Serializable]
- public class C
- {
- public D D;
- }
- [Serializable]
- public class D
- {
- public E E;
- }
- [Serializable]
- public class E
- {
- public F F;
- }
- [Serializable]
- public class F
- {
- public G G;
- }
- [Serializable]
- public class G
- {
- public H H;
- }
- [Serializable]
- public class H
- {
- public I I;
- }
- [Serializable]
- public class I
- {
- public int Val;
- }
- class TestScript : MonoBehaviour
- {
- string _report = "";
- void Start ()
- {
- var emptyClass = new EmptyClass();
- var smallString = new StringOnly { String = "hey" };
- var largeString = new StringOnly { String = new string('*', 100000) };
- var smallArray = new ArrayOnly { IntArray = new[] { 1, 2, 3 } };
- var largeArray = new ArrayOnly { IntArray = new int[1000] };
- var shallowNestedObject = new A { B = new B { C = new C() { } } };
- var deepNestedObject = new A
- {
- B = new B
- {
- C = new C
- {
- D = new D
- {
- E = new E
- {
- F = new F
- {
- G = new G
- {
- H = new H
- {
- I = new I { Val = 123 }
- }
- }
- }
- }
- }
- }
- }
- };
- Test("Empty Class", emptyClass, 100000);
- Test("Small String", smallString, 10000);
- Test("Large String", largeString, 1000);
- Test("Small Array", smallArray, 100000);
- Test("Large Array", largeArray, 1000);
- Test("Shallow Nested Object", shallowNestedObject, 10000);
- Test("Deep Nested Object", deepNestedObject, 10000);
- }
- void Test<T> (string label, T obj, int reps)
- where T : class
- {
- // Warm up reflection
- JsonUtility.ToJson(obj);
- LitJson.JsonMapper.ToJson(obj);
- JsonConvert.SerializeObject(obj);
- Tiny.Json.Encode(obj);
- SerializationUtility.SerializeValue(obj, DataFormat.JSON);
- var stopwatch = new System.Diagnostics.Stopwatch();
- string unityJson = null;
- string litJsonJson = null;
- string jsonDotNetJson = null;
- string tinyJson = null;
- byte[] odinJson = null;
- T unityObj = null;
- T litJsonObj = null;
- T jsonDotNetObj = null;
- T tinyJsonObj = null;
- T odinObj = null;
- stopwatch.Start();
- for ( var i = 0; i < reps; ++i )
- {
- unityJson = JsonUtility.ToJson(obj);
- }
- var unitySerializeTime = stopwatch.ElapsedMilliseconds;
- stopwatch.Reset();
- stopwatch.Start();
- for ( var i = 0; i < reps; ++i )
- {
- litJsonJson = LitJson.JsonMapper.ToJson(obj);
- }
- var litJsonSerializeTime = stopwatch.ElapsedMilliseconds;
- stopwatch.Reset();
- stopwatch.Start();
- for ( var i = 0; i < reps; ++i )
- {
- tinyJson = Tiny.Json.Encode(obj);
- }
- var tinyJsonSerializeTime = stopwatch.ElapsedMilliseconds;
- stopwatch.Reset();
- stopwatch.Start();
- for ( var i = 0; i < reps; ++i )
- {
- jsonDotNetJson = JsonConvert.SerializeObject(obj);
- }
- var jsonDotNetSerializeTime = stopwatch.ElapsedMilliseconds;
- stopwatch.Reset();
- stopwatch.Start();
- for (var i = 0; i < reps; ++i)
- {
- odinJson = SerializationUtility.SerializeValue(obj, DataFormat.JSON);
- }
- var odinSerializeTime = stopwatch.ElapsedMilliseconds;
- stopwatch.Reset();
- stopwatch.Start();
- for ( var i = 0; i < reps; ++i )
- {
- unityObj = JsonUtility.FromJson<T>(unityJson);
- }
- var unityDeserializeTime = stopwatch.ElapsedMilliseconds;
- stopwatch.Reset();
- stopwatch.Start();
- for ( var i = 0; i < reps; ++i )
- {
- litJsonObj = LitJson.JsonMapper.ToObject<T>(litJsonJson);
- }
- var litJsonDeserializeTime = stopwatch.ElapsedMilliseconds;
- stopwatch.Reset();
- stopwatch.Start();
- for ( var i = 0; i < reps; ++i )
- {
- tinyJsonObj = Tiny.Json.Decode<T>(tinyJson);
- }
- var tinyJsonDeserializeTime = stopwatch.ElapsedMilliseconds;
- stopwatch.Reset();
- stopwatch.Start();
- for ( var i = 0; i < reps; ++i )
- {
- jsonDotNetObj = JsonConvert.DeserializeObject<T>(jsonDotNetJson);
- }
- var jsonDotNetDeserializeTime = stopwatch.ElapsedMilliseconds;
- stopwatch.Reset();
- stopwatch.Start();
- for (var i = 0; i < reps; ++i)
- {
- odinObj = SerializationUtility.DeserializeValue<T>(odinJson, DataFormat.JSON);
- }
- var odinDeserializeTime = stopwatch.ElapsedMilliseconds;
- var unitySize = Encoding.UTF8.GetBytes(unityJson).Length;
- var litJsonSize = Encoding.UTF8.GetBytes(litJsonJson).Length;
- var jsonDotNetSize = Encoding.UTF8.GetBytes(jsonDotNetJson).Length;
- var tinyJsonSize = Encoding.UTF8.GetBytes(tinyJson).Length;
- var odinSize = odinJson.Length;
- var msg = string.Format(
- "{0} ({1} reps)\n" +
- "Library,\tSize,\tSerializeTime,\tDeserialize Time\n" +
- "Unity,\t{2},\t{3},\t\t{4}\n" +
- "LitJSON,\t{5},\t{6},\t\t{7}\n" +
- "Json.NET,\t{8},\t{9},\t\t{10}\n" +
- "TinyJson,\t{11},\t{12},\t\t{13}\n" +
- "Odin,\t{14},\t{15},\t\t{16}\n",
- label, reps,
- unitySize, unitySerializeTime, unityDeserializeTime,
- litJsonSize, litJsonSerializeTime, litJsonDeserializeTime,
- jsonDotNetSize, jsonDotNetSerializeTime, jsonDotNetDeserializeTime,
- tinyJsonSize,tinyJsonSerializeTime,tinyJsonDeserializeTime,
- odinSize, odinSerializeTime, odinDeserializeTime
- );
- _report += msg;
- }
- void OnGUI ()
- {
- GUI.TextArea(new Rect(0, 0, Screen.width, Screen.height), _report);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement