JSON序列化现在应用非常多,尤其在前后端分离的情况下,平常大多数C#下都使用Newtonsoft.Json来操作,量少的情况下,还可以忽略,但量大的情况下就要考虑使用ServiceStack.Text来操作,序列化性能差不多,反序列化性能要高一倍左右
int count = 10000; User user = new User() { Id = 1, Name = "tyh", AddTime = DateTime.Now, Age = 15, Status = true }; string temp = String.Empty; User user1 = new User(); string temp1 = "{\"Id\": \"2\",\"Name\": \"xxc\",\"AddTime\": \"2016-09-07 10:10:10\",\"Age\": \"15\",\"Status\": \"true\"}"; Stopwatch sw = new Stopwatch(); sw.Restart(); for (int i = 0; i < count; i++) { temp = ServiceStack.Text.JsonSerializer.SerializeToString(user); } sw.Stop(); Response.Write("ServiceStack-Ser:" + sw.ElapsedMilliseconds + "</br>"); sw.Restart(); for (int i = 0; i < count; i++) { user1 = ServiceStack.Text.JsonSerializer.DeserializeFromString<User>(temp1); } sw.Stop(); Response.Write("ServiceStack-Des:" + sw.ElapsedMilliseconds + "</br>"); sw.Restart(); for (int i = 0; i < count; i++) { temp = Newtonsoft.Json.JsonConvert.SerializeObject(user); } sw.Stop(); Response.Write("Newtonsoft-Ser:" + sw.ElapsedMilliseconds + "</br>"); sw.Restart(); for (int i = 0; i < count; i++) { user1 = Newtonsoft.Json.JsonConvert.DeserializeObject<User>(temp1); } sw.Stop(); Response.Write("Newtonsoft-Des:" + sw.ElapsedMilliseconds + "</br>"); Response.End();
以下是测试结果,我只截一个图,其他自行测试吧
时间: 2024-11-07 09:01:01