C# Serialization performance in System.Runtime.Serialization.Formatters.Binary.BinaryFormatter,Newtonsoft.Json.JsonConvert and System.Text.Json.JsonSe
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}"); } } } }
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 .
下一篇: 吃炒芝麻的好处有哪些,这些好处你都知道吗