微软自带的Serialization和Newtonsoft简单测试

刚刚对这两个进行了一下小小的测试

发现

当转换的内容少的时候  微软自带的比Newtonsoft速度要快一些,内容多的时候反之,当内容多到一定量的时候微软自带的就不能转换了,需要修改一下MaxJsonLength的默认值,此处我改为:999999999,这个时候可以转换了,不过时间很长。

添加50个对象:

添加500个对象:

添加5K对象:

添加5W对象:

添加50W对象:

50W,修改完默认值:

添加500W 对象:

添加700W对象:

微软自带的出不来了,报错了,这次我是把Newtonsoft移到上边了

添加900W对象:

图1

图2

代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using System.Collections;
using System.Diagnostics;
using System.Web.Script.Serialization;

namespace JsonNet
{
    class Program
    {
        static void Main(string[] args)
        {
            Person p1 = new Person() { age = 23, name = "张三", sex = "N" };

            List<Person> arr = new List<Person>();
            for (int i = 0; i < 5000000; i++)
            {
                arr.Add(p1);

            }
            Stopwatch watch1 = new Stopwatch();
            Stopwatch watch2 = new Stopwatch();


            watch1.Start();
            JavaScriptSerializer jsSerializer = new JavaScriptSerializer();
            jsSerializer.MaxJsonLength = 999999999;
            string str = jsSerializer.Serialize(arr);
            Console.WriteLine("Serialization转换时间:" + watch1.Elapsed);


            watch2.Start();
            string strr = JsonConvert.SerializeObject(arr);
            Console.Write("Newtonsoft转换时间" + watch2.Elapsed);

            Console.ReadKey();

        }
    }

    public class Person
    {
        public string name;
        public int age;
        public string sex;

    }
}
原文地址:https://www.cnblogs.com/xiaoshi657/p/4617353.html