using ConsoleApp390.Model; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; using System.Runtime.Serialization; using System.Runtime.Serialization.Formatters.Binary; using System.Text; using System.Threading; using System.Threading.Tasks; namespace ConsoleApp390 { class Program { static void Main(string[] args) { SerializationDemo(); } static void SerializationDemo() { List<SalesOrderDetail> dataList = GetList(); bool IsBusy = false; for(int i=0;i<10;i++) { if(!IsBusy) { IsBusy = true; Task task = Task.Run(() => { TestCostDemo(dataList); }); task.Wait(); Console.WriteLine($"Task {i} has finished!"); IsBusy = false; } } } static void TestCostDemo(List<SalesOrderDetail> dataList) { StringBuilder msgBuilder = new StringBuilder(); Stopwatch sw = new Stopwatch(); sw.Start(); Console.WriteLine($"BinaryFormatterDemo started at {DateTime.Now.ToString("yyyyMMddHHmmssffff")}"); BinaryFormatterDemo<SalesOrderDetail>(dataList); sw.Stop(); Console.WriteLine($"BinaryFormatterDemo stopped at {DateTime.Now.ToString("yyyyMMddHHmmssffff")}"); msgBuilder.Append($"In .net 4.8,now {DateTime.Now.ToString("yyyyMMddHHmmssffff")} BinaryFormatterDemo cost {sw.ElapsedMilliseconds} milliseconds."); sw.Restart(); Console.WriteLine($"NewtonJsonDemo started at {DateTime.Now.ToString("yyyyMMddHHmmssffff")}"); NewtonJsonDemo<SalesOrderDetail>(dataList); sw.Stop(); Console.WriteLine($"NewtonJsonDemo stopped at {DateTime.Now.ToString("yyyyMMddHHmmssffff")}"); msgBuilder.Append($"NewtonJsonDemo cost {sw.ElapsedMilliseconds} milliseconds."); sw.Restart(); Console.WriteLine($"JsonSerializerDemo started at {DateTime.Now.ToString("yyyyMMddHHmmssffff")}"); JsonSerializerDemo<SalesOrderDetail>(dataList); sw.Stop(); Console.WriteLine($"JsonSerializerDemo stopped at {DateTime.Now.ToString("yyyyMMddHHmmssffff")}"); msgBuilder.AppendLine($"JsonSerializerDemo cost {sw.ElapsedMilliseconds} milliseconds.\n\n"); string msg = msgBuilder.ToString(); PrintMsg(msg); } private static void PrintMsg(string msg) { Console.WriteLine(msg); Debug.WriteLine(msg); LogResult(msg); } static List<SalesOrderDetail> GetList() { using (AdventureWorks2017Entities db = new AdventureWorks2017Entities()) { List<SalesOrderDetail> dataList = new List<SalesOrderDetail>(db.SalesOrderDetails); return dataList; } } static void BinaryFormatterDemo<T>(List<T> dataList) { using (FileStream fs = new FileStream($"{Guid.NewGuid().ToString().Substring(0, 6)}BinFormatter.dat", FileMode.Create)) { BinaryFormatter binFormatter = new BinaryFormatter(); try { binFormatter.Serialize(fs, dataList); } catch (SerializationException e) { Console.WriteLine($"BinaryFormatter serialize ex:{e.StackTrace}"); } } } static void NewtonJsonDemo<T>(List<T> dataList) { try { string jsonString = JsonConvert.SerializeObject(dataList); } catch (SerializationException ex) { Console.WriteLine($"NewtonJson serialization ex :{ex.StackTrace}"); } } static void JsonSerializerDemo<T>(List<T> dataList) { try { string msJsonSerializerString = System.Text.Json.JsonSerializer.Serialize(dataList); } catch (SerializationException ex) { Console.WriteLine($"System.Text.Json.JsonSerializer ex:{ex.StackTrace}"); } } static void LogResult(string msg) { string logPath = @"log.txt"; using (StreamWriter logWriter = new StreamWriter(logPath, true, Encoding.UTF8)) { logWriter.WriteLine(msg); } } } }
I had validated in .net 4.8 NewtonSoft.Json's speed rank 1st,System.Text.Json.JsonSerializer.Serialize 2nd,and BinaryFormatter 3rd.
In .net 4.8,now 201911231857080779 BinaryFormatterDemo cost 1679 milliseconds.NewtonJsonDemo cost 1182 milliseconds.JsonSerializerDemo cost 1387 milliseconds. In .net 4.8,now 201911231857123745 BinaryFormatterDemo cost 1686 milliseconds.NewtonJsonDemo cost 823 milliseconds.JsonSerializerDemo cost 947 milliseconds. In .net 4.8,now 201911231857158273 BinaryFormatterDemo cost 1676 milliseconds.NewtonJsonDemo cost 797 milliseconds.JsonSerializerDemo cost 992 milliseconds. In .net 4.8,now 201911231857193271 BinaryFormatterDemo cost 1704 milliseconds.NewtonJsonDemo cost 786 milliseconds.JsonSerializerDemo cost 987 milliseconds. In .net 4.8,now 201911231857227811 BinaryFormatterDemo cost 1672 milliseconds.NewtonJsonDemo cost 777 milliseconds.JsonSerializerDemo cost 983 milliseconds. In .net 4.8,now 201911231857261715 BinaryFormatterDemo cost 1582 milliseconds.NewtonJsonDemo cost 840 milliseconds.JsonSerializerDemo cost 985 milliseconds. In .net 4.8,now 201911231857296400 BinaryFormatterDemo cost 1642 milliseconds.NewtonJsonDemo cost 828 milliseconds.JsonSerializerDemo cost 973 milliseconds. In .net 4.8,now 201911231857331241 BinaryFormatterDemo cost 1664 milliseconds.NewtonJsonDemo cost 786 milliseconds.JsonSerializerDemo cost 1006 milliseconds. In .net 4.8,now 201911231857366239 BinaryFormatterDemo cost 1698 milliseconds.NewtonJsonDemo cost 762 milliseconds.JsonSerializerDemo cost 999 milliseconds. In .net 4.8,now 201911231857400455 BinaryFormatterDemo cost 1661 milliseconds.NewtonJsonDemo cost 780 milliseconds.JsonSerializerDemo cost 992 milliseconds.