C# Serialization performance in System.Runtime.Serialization.Formatters.Binary.BinaryFormatter,Newto

2021-01-28 03:14

阅读:452

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 dataList = GetList();
StringBuilder msgBuilder = new StringBuilder();
Stopwatch sw = new Stopwatch();
//sw.Start();
//Console.WriteLine($"BinaryFormatterDemo started at {DateTime.Now.ToString("yyyyMMddHHmmssffff")}");
//BinaryFormatterDemo(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(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(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 GetList()
{
using(AdventureWorks2017Context db=new AdventureWorks2017Context())
{
List dataList = new List(db.SalesOrderDetail);
return dataList;
}
}


static void BinaryFormatterDemo(List 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(List dataList)
{
try
{
string jsonString = JsonConvert.SerializeObject(dataList);
}
catch(SerializationException ex)
{
Console.WriteLine($"NewtonJson serialization ex :{ex.StackTrace}");
}
}


static void JsonSerializerDemo(List 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(List dataList)
//{
// using (var file = File.Create("person.bin"))
// {
// ProtoBuf.Serializer.Serialize(file, dataList);
// }
//}


}
}

 


评论


亲,登录后才可以留言!