In .net 4.8,calculate the time cost of serialization in BinaryFormatter,NewtonSoft.json,and System.Text.Json.JsonSerializer.Serialize
程序员文章站
2022-06-13 07:56:01
I had validated in .net 4.8 NewtonSoft.Json's speed rank 1st,System.Text.Json.JsonSerializer.Serialize 2nd,and BinaryFormatter 3rd. ......
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.
上一篇: 刘子业荒诞的一生:霸占姑姑,死于叔叔之手
下一篇: sklearn随笔-归一化(标准化)