比较Json.Net和fastJson的效率
var zoo1 = new zoo(); zoo1.animals = new List<animal>(); zoo1.animals.Add(new cat()); zoo1.animals.Add(new dog()); Stopwatch sw1 = new Stopwatch(); sw1.Start(); string ss = ""; for (int i = 0; i < 1000000; i++) { ss = Newtonsoft.Json.JsonConvert.SerializeObject(zoo1); } sw1.Stop(); Console.WriteLine("Newtonsoft s1:" + sw1.ElapsedMilliseconds); sw1.Restart(); sw1.Start(); zoo zoo2 = null; for (int i = 0; i < 1000000; i++) { zoo2 = Newtonsoft.Json.JsonConvert.DeserializeObject<zoo>(ss); } sw1.Stop(); Console.WriteLine("Newtonsoft d1:" + sw1.ElapsedMilliseconds); sw1.Restart(); string ss1 = ""; sw1.Start(); for (int i = 0; i < 1000000; i++) { ss1 = fastJSON.JSON.ToJSON(zoo1); } sw1.Stop(); Console.WriteLine("fastJSON d1:" + sw1.ElapsedMilliseconds); sw1.Restart(); sw1.Start(); zoo zoo3 = null; for (int i = 0; i < 1000000; i++) { zoo3 = fastJSON.JSON.ToObject<zoo>(ss1); } sw1.Stop(); Console.WriteLine("fastJSON d1:" + sw1.ElapsedMilliseconds); Thread.Sleep(-1);
release模式
debug模式
附注. fastJson还有特性,反序列化之后的zoo3中的两个animal的类型分别为cat和dog。但是其他的json'库都没有实现这个特性。