using System;using System.Collections.Generic;using System.Collections;using System.IO;using System.Runtime.Serialization.Formatters.Binary;using Syst ...
In .net core 3.0
using System;
using System.Collections.Generic;
using System.Collections;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
using Newtonsoft.Json;
using System.Text;
using System.Text.Json;
using Google.Protobuf;
using System.Diagnostics;
using ConsoleApp389.Models;
namespace ConsoleApp389
{
class Program
{
static void Main(string[] args)
{
TestCostDemo();
Console.ReadLine();
}
static void TestCostDemo()
{
List<SalesOrderDetail> dataList = GetList();
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.AppendLine($"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.AppendLine($"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.");
string msg = msgBuilder.ToString();
Console.WriteLine(msg);
System.Diagnostics.Debug.WriteLine(msg);
}
static List<SalesOrderDetail> GetList()
{
using(AdventureWorks2017Context db=new AdventureWorks2017Context())
{
List<SalesOrderDetail> dataList = new List<SalesOrderDetail>(db.SalesOrderDetail);
return dataList;
}
}
static void BinaryFormatterDemo<T>(List<T> dataList)
{
using (FileStream fs = new FileStream($"{Guid.NewGuid().ToString().Substring(0,6)}.bin", 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 GoogleProtoBufDemo<T>(List<T> dataList)
//{
// using (var file = File.Create("person.bin"))
// {
// ProtoBuf.Serializer.Serialize<T>(file, dataList);
// }
//}
}
}
In .net 4.8
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.Tasks; namespace ConsoleApp390 { class Program { static void Main(string[] args) { TestCostDemo(); Console.ReadLine(); } static void TestCostDemo() { List<SalesOrderDetail> dataList = GetList(); 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.AppendLine($"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.AppendLine($"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."); string msg = msgBuilder.ToString(); Console.WriteLine(msg); System.Diagnostics.Debug.WriteLine(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}"); } } } }
Test them in program.
using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; namespace ConsoleApp391 { class Program { static void Main(string[] args) { TestNet48Demo(); if(IsFirstFinished) { TestCore30Demo(); } } static bool IsBusy= false; static bool IsFirstFinished = false; static void TestNet48Demo() { for (int i = 0; i < 10; i++) { if (!IsBusy) { Console.WriteLine($"Test Net48Demo() {i}"); IsBusy = true; Thread thread = new Thread(() => { Net48Demo(); }); thread.Start(); thread.Join(); while(thread.ThreadState!=System.Threading.ThreadState.Stopped) {} if(thread.ThreadState==System.Threading.ThreadState.Stopped) { IsBusy = false; } } } IsFirstFinished = true; } static void Net48Demo() { string net48Name = @"D:\C\ConsoleApp390\bin\Debug\ConsoleApp390.exe"; Process proc=Process.Start(net48Name); proc.WaitForExit(); } static void TestCore30Demo() { for (int i = 0; i < 10; i++) { if (!IsBusy) { Console.WriteLine($"Test Core30Demo() {i}"); IsBusy = true; Thread thread = new Thread(() => { Core30Demo(); }); thread.Start(); thread.Join(); while (thread.ThreadState != System.Threading.ThreadState.Stopped) { } if (thread.ThreadState == System.Threading.ThreadState.Stopped) { IsBusy = false; } } } } static void Core30Demo() { string core30Name = @"D:\C\ConsoleApp389\bin\Debug\netcoreapp3.0\ConsoleApp389.exe"; Process proc=Process.Start(core30Name); proc.WaitForExit(); } } }
I'll list serveral executed results.
In .net NewtonJson is faster than System.Text.Json.JsonSerializer.Serialize.The validated result as below.
1.
NewtonJsonDemo cost 1290 milliseconds.
JsonSerializerDemo cost 1404 milliseconds.
2.
NewtonJsonDemo cost 1213 milliseconds.
JsonSerializerDemo cost 1814 milliseconds.
3.
NewtonJsonDemo cost 1305 milliseconds.
JsonSerializerDemo cost 1373 milliseconds.
4.
NewtonJsonDemo cost 1329 milliseconds.
JsonSerializerDemo cost 1749 milliseconds.
5.
NewtonJsonDemo cost 1234 milliseconds.
JsonSerializerDemo cost 1395 milliseconds.
While in .net core 3.0 System.Text.Json.JsonSerializer.Serialize is faster than NewtonJson.
1.
NewtonJsonDemo cost 1704 milliseconds.
JsonSerializerDemo cost 1247 milliseconds.
2.
NewtonJsonDemo cost 1596 milliseconds.
JsonSerializerDemo cost 1451 milliseconds.
3.
NewtonJsonDemo cost 1430 milliseconds.
JsonSerializerDemo cost 1221 milliseconds.
4.
NewtonJsonDemo cost 1390 milliseconds.
JsonSerializerDemo cost 1219 milliseconds.
5.
NewtonJsonDemo cost 1334 milliseconds.
JsonSerializerDemo cost 1216 milliseconds.
Totally speaking, the .net core is completely faster than .net both in NewtonJson and System.Text.Json.JsonSerializer.Serialize
The data source come from Adventureworks2017.Sales.SalesOrderDetail table which has 121317 rows .
In .net 4.8,now 201911221845464103 NewtonJsonDemo cost 1147 milliseconds.JsonSerializerDemo cost 1352 milliseconds. In .net 4.8,now 201911221845548941 NewtonJsonDemo cost 1082 milliseconds.JsonSerializerDemo cost 1263 milliseconds. In .net 4.8,now 201911221846033311 NewtonJsonDemo cost 1143 milliseconds.JsonSerializerDemo cost 1245 milliseconds. In .net 4.8,now 201911221846149554 NewtonJsonDemo cost 1109 milliseconds.JsonSerializerDemo cost 1210 milliseconds. In .net 4.8,now 201911221846233923 NewtonJsonDemo cost 1110 milliseconds.JsonSerializerDemo cost 1299 milliseconds. In .net 4.8,now 201911221846318762 NewtonJsonDemo cost 1140 milliseconds.JsonSerializerDemo cost 1253 milliseconds. In .net 4.8,now 201911221846403287 NewtonJsonDemo cost 1125 milliseconds.JsonSerializerDemo cost 1192 milliseconds. In .net 4.8,now 201911221846487188 NewtonJsonDemo cost 1215 milliseconds.JsonSerializerDemo cost 1219 milliseconds. In .net 4.8,now 201911221846569683 NewtonJsonDemo cost 1126 milliseconds.JsonSerializerDemo cost 1306 milliseconds. In .net 4.8,now 201911221847057021 NewtonJsonDemo cost 1121 milliseconds.JsonSerializerDemo cost 1322 milliseconds. In .core 3.0,now 201911221847194044 NewtonJsonDemo cost 1172 milliseconds.JsonSerializerDemo cost 999 milliseconds. In .core 3.0,now 201911221847269311 NewtonJsonDemo cost 1133 milliseconds.JsonSerializerDemo cost 1069 milliseconds. In .core 3.0,now 201911221847343703 NewtonJsonDemo cost 1043 milliseconds.JsonSerializerDemo cost 941 milliseconds. In .core 3.0,now 201911221847418622 NewtonJsonDemo cost 1098 milliseconds.JsonSerializerDemo cost 919 milliseconds. In .core 3.0,now 201911221847492909 NewtonJsonDemo cost 1072 milliseconds.JsonSerializerDemo cost 1025 milliseconds. In .core 3.0,now 201911221847567419 NewtonJsonDemo cost 1073 milliseconds.JsonSerializerDemo cost 1063 milliseconds. In .core 3.0,now 201911221848041989 NewtonJsonDemo cost 1059 milliseconds.JsonSerializerDemo cost 1002 milliseconds. In .core 3.0,now 201911221848125705 NewtonJsonDemo cost 1112 milliseconds.JsonSerializerDemo cost 880 milliseconds. In .core 3.0,now 201911221848242935 NewtonJsonDemo cost 1116 milliseconds.JsonSerializerDemo cost 884 milliseconds. In .core 3.0,now 201911221848318144 NewtonJsonDemo cost 1089 milliseconds.JsonSerializerDemo cost 991 milliseconds.