欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C# Serialization performance in System.Runtime.Serialization.Formatters.Binary.BinaryFormatter,Newtonsoft.Json.JsonConvert and System.Text.Json.JsonSe

程序员文章站 2022-06-29 20:34:13
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}");
            }
        }
    }
}

 

 

 

 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 .