private static void bin_deserialize() { Console.WriteLine(); Console.Write("bin deserialize"); ColClass c = CreateObject(); var stopwatch = new Stopwatch(); for (int pp = 0; pp < tcount; pp++) { BinaryFormatter bf = new BinaryFormatter(); MemoryStream ms = new MemoryStream(); ColClass deserializedStore = null; stopwatch.Restart(); bf.Serialize(ms, c); //Console.WriteLine(" size = " +ms.Length); for (int i = 0; i < count; i++) { stopwatch.Stop(); // we stop then resume the stopwatch here so we don't factor in Seek()'s execution ms.Seek(0L, SeekOrigin.Begin); stopwatch.Start(); deserializedStore = (ColClass)bf.Deserialize(ms); } stopwatch.Stop(); Console.Write("\t" + stopwatch.ElapsedMilliseconds); } }
//private static string pser(object data) //{ // System.Drawing.Point p = (System.Drawing.Point)data; // return p.X.ToString() + "," + p.Y.ToString(); //} //private static object pdes(string data) //{ // string[] ss = data.Split(','); // return new System.Drawing.Point( // int.Parse(ss[0]), // int.Parse(ss[1]) // ); //} //private static string tsser(object data) //{ // return ((TimeSpan)data).Ticks.ToString(); //} //private static object tsdes(string data) //{ // return new TimeSpan(long.Parse(data)); //} public static ColClass CreateObject() { var c = new ColClass(); c.BooleanValue = true; c.OrdinaryDecimal = 3; if (exotic) { c.NullableGuid = Guid.NewGuid(); c.Hash = new Hashtable(); c.Bytes = new byte[1024]; c.StringDictionary = new Dictionary <string, BaseClass>(); c.ObjectDictionary = new Dictionary <BaseClass, BaseClass>(); c.IntDictionary = new Dictionary <int, BaseClass>(); c.NullableDouble = 100.003; if (dsser) { c.Dataset = ds; } c.NullableDecimal = 3.14M; c.Hash.Add(new Class1("0", "hello", Guid.NewGuid()), new Class2("1", "code", "desc")); c.Hash.Add(new Class2("0", "hello", "pppp"), new Class1("1", "code", Guid.NewGuid())); c.StringDictionary.Add("name1", new Class2("1", "code", "desc")); c.StringDictionary.Add("name2", new Class1("1", "code", Guid.NewGuid())); c.IntDictionary.Add(1, new Class2("1", "code", "desc")); c.IntDictionary.Add(2, new Class1("1", "code", Guid.NewGuid())); c.ObjectDictionary.Add(new Class1("0", "hello", Guid.NewGuid()), new Class2("1", "code", "desc")); c.ObjectDictionary.Add(new Class2("0", "hello", "pppp"), new Class1("1", "code", Guid.NewGuid())); c.ArrayType = new BaseClass[2]; c.ArrayType[0] = new Class1(); c.ArrayType[1] = new Class2(); } c.Items.Add(new Class1("1", "1", Guid.NewGuid())); c.Items.Add(new Class2("2", "2", "desc1")); c.Items.Add(new Class1("3", "3", Guid.NewGuid())); c.Items.Add(new Class2("4", "4", "desc2")); c.Laststring = "" + DateTime.Now; return(c); }
private static void fastjson_serialize() { Console.WriteLine(); Console.Write("fastjson serialize"); ColClass c = CreateObject(); var stopwatch = new Stopwatch(); for (int pp = 0; pp < tcount; pp++) { string jsonText = null; stopwatch.Restart(); for (int i = 0; i < count; i++) { jsonText = fastJSON.JSON.ToJSON(c); } stopwatch.Stop(); Console.Write("\t" + stopwatch.ElapsedMilliseconds); } }
private static void bin_serialize() { Console.Write("\r\nbin serialize"); ColClass c = CreateObject(); var stopwatch = new Stopwatch(); for (int pp = 0; pp < tcount; pp++) { BinaryFormatter bf = new BinaryFormatter(); MemoryStream ms = new MemoryStream(); stopwatch.Restart(); for (int i = 0; i < count; i++) { stopwatch.Stop(); // we stop then resume the stop watch here so we don't factor in the MemoryStream()'s execution ms = new MemoryStream(); stopwatch.Start(); bf.Serialize(ms, c); } stopwatch.Stop(); Console.Write("\t" + stopwatch.ElapsedMilliseconds); } }
private static void fastjson_deserialize() { Console.WriteLine(); Console.Write("fastjson deserialize"); ColClass c = CreateObject(); var stopwatch = new Stopwatch(); for (int pp = 0; pp < tcount; pp++) { ColClass deserializedStore; string jsonText = null; stopwatch.Restart(); jsonText = fastJSON.JSON.ToJSON(c); //Console.WriteLine(" size = " + jsonText.Length); for (int i = 0; i < count; i++) { deserializedStore = (ColClass)fastJSON.JSON.ToObject(jsonText); } stopwatch.Stop(); Console.Write("\t" + stopwatch.ElapsedMilliseconds); } }