剛剛對這兩個進行了一下小小的測試
發現
當轉換的內容少的時候 微軟自帶的比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; } }