C# Serialization performance in System.Runtime.Serialization.Formatters.Binary.BinaryFormatter,Newto
2021-01-28 03:14
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
StringBuilder msgBuilder = new StringBuilder();
Stopwatch sw = new Stopwatch();
//sw.Start();
//Console.WriteLine($"BinaryFormatterDemo started at {DateTime.Now.ToString("yyyyMMddHHmmssffff")}");
//BinaryFormatterDemo
//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
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
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
{
using(AdventureWorks2017Context db=new AdventureWorks2017Context())
{
List
return dataList;
}
}
static void BinaryFormatterDemo
{
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
{
try
{
string jsonString = JsonConvert.SerializeObject(dataList);
}
catch(SerializationException ex)
{
Console.WriteLine($"NewtonJson serialization ex :{ex.StackTrace}");
}
}
static void JsonSerializerDemo
{
try
{
string msJsonSerializerString = System.Text.Json.JsonSerializer.Serialize(dataList);
}
catch(SerializationException ex)
{
Console.WriteLine($"System.Text.Json.JsonSerializer ex:{ex.StackTrace}");
}
}
//static void GoogleProtoBufDemo
//{
// using (var file = File.Create("person.bin"))
// {
// ProtoBuf.Serializer.Serialize
// }
//}
}
}
上一篇:Docker配置yapi接口
文章标题:C# Serialization performance in System.Runtime.Serialization.Formatters.Binary.BinaryFormatter,Newto
文章链接:http://soscw.com/index.php/essay/48044.html