幾個常用Json元件的效能測試
https://www.cnblogs.com/blqw/p/3274229.html
上一篇文章中我已經介紹了JsonBuilder方案的整體思路以及一個版本的雛形程式碼,他現在已經是可以使用的了,但是因為是實時反射的,所以效率並不高。
鑑於幾位博友對Json轉換元件的效能有興趣,我先放幾個常用元件的測試結果以供參考。下一篇在詳細介紹JsonBuilder的優化工作
測試用例
測試用例 測試程式碼測試方案1:微軟自帶的的 JavaScriptSerializer
引用元件:System.Web.Extensions(微軟自帶了,引用下就好)
string str = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(User.TestUser()); //{"Self":null,"UID":"6f50b429-5c13-4875-a29a-e4bd8d7b2772","Name":"blqw","Birthday":"\/Date(530964000000)\/","Sex":0,"IsDeleted":false,"LoginHistory":["\/Date(1377129600000)\/","\/Date(1377137410000)\/","\/Date(1377146036000)\/","\/Date(1377163518000)\/","\/Date(1377184019000)\/"],"Info":{"Address":"廣東省廣州市","Phone":{"手機":"18688888888","電話":"82580000","短號":"10086","QQ":"21979018"},"ZipCode":510000},"Double":-Infinity}
測試方案2:Newtonsoft.Json
引用元件:Newtonsoft.Json.Net35.dll
string str = Newtonsoft.Json.JsonConvert.SerializeObject(User.TestUser()); //{"Self":null,"UID":"6f50b429-5c13-4875-a29a-e4bd8d7b2772","Name":"blqw","Birthday":"\/Date(530964000000+0800)\/","Sex":0,"IsDeleted":false,"LoginHistory":["\/Date(1377129600000+0800)\/","\/Date(1377137410000+0800)\/","\/Date(1377146036000+0800)\/","\/Date(1377163518000+0800)\/","\/Date(1377184019000+0800)\/"],"Info":{"Address":"廣東省廣州市","Phone":{"手機":"18688888888","電話":"82580000","短號":"10086","QQ":"21979018"},"ZipCode":510000},"Double":-Infinity}
測試方案3:Jayrock.Json
引用元件:Jayrock.Json.dll
JsonTextWriter jtw = new Jayrock.Json.JsonTextWriter(); Jayrock.Json.Conversion.JsonConvert.Export(User.TestUser(), jtw); string str = jtw.ToString(); //{"uID":"6f50b429-5c13-4875-a29a-e4bd8d7b2772","name":"blqw","birthday":"1986-10-29T18:00:00.0000000+08:00","sex":"Male","isDeleted":false,"loginHistory":["2013-08-22T08:00:00.0000000+08:00","2013-08-22T10:10:10.0000000+08:00","2013-08-22T12:33:56.0000000+08:00","2013-08-22T17:25:18.0000000+08:00","2013-08-22T23:06:59.0000000+08:00"],"info":{"address":"廣東省廣州市","phone":{"手機":"18688888888","電話":"82580000","短號":"10086","QQ":"21979018"},"zipCode":510000},"double":-Infinity}
測試方案4:fastJSON.NET
引用元件:fastJSON.dll
JSONParameters p = new JSONParameters(); p.EnableAnonymousTypes = p.IgnoreCaseOnDeserialize = p.ShowReadOnlyProperties = p.UseEscapedUnicode = p.UseExtensions = p.UseFastGuid = p.UseOptimizedDatasetSchema = p.UseUTCDateTime = p.UsingGlobalTypes = false;//所有引數為false,執行速度最快 string str = fastJSON.JSON.Instance.ToJSON(User.TestUser(), p); //{"Self":null,"UID":"6f50b429-5c13-4875-a29a-e4bd8d7b2772","Name":"blqw","Birthday":"1986-10-29 18:00:00","Sex":"Male","IsDeleted":false,"LoginHistory":["2013-08-22 08:00:00","2013-08-22 10:10:10","2013-08-22 12:33:56","2013-08-22 17:25:18","2013-08-22 23:06:59"],"Info":{"Address":"廣東省廣州市","Phone":{"手機":"18688888888","電話":"82580000","短號":"10086","QQ":"21979018"},"ZipCode":510000},"Double":-Infinity}
測試方案5,6:QuickJsonBuilder,JsonBuilder
ps:以上幾種型別都不支援迴圈引用,如果有,直接丟擲異常
說明:QuickJsonBuilder就是我上篇中介紹的JsonBuilder的終極形態
QuickJsonBuilder jb = new QuickJsonBuilder(); jb.ToJson(User.TestUser());
//{"Self":null,"UID":"6f50b429-5c13-4875-a29a-e4bd8d7b2772","Name":"blqw","Birthday":"1986-10-29 18:00:00","Sex":"Male","IsDeleted":false,"LoginHistory":["2013-08-22 08:00:00","2013-08-22 10:10:10","2013-08-22 12:33:56","2013-08-22 17:25:18","2013-08-22 23:06:59"],"Info":{"Address":"廣東省廣州市","Phone":{"手機":"18688888888","電話":"82580000","短號":"10086","QQ":"21979018"},"ZipCode":510000},"Double":-Infinity}
ps:當Self指向自己本身的時候,將返回 "Self":undefined 用以區分空物件
測試方案7:ServiceStack.Text
引用元件:ServiceStack.Text.dll 抱歉我只找到4.0的
這個是後來補上的,測試程式碼中沒有這個...
User.TestUser().ToJson(); //{"UID":"48d4624ad0c8475094d00fc769c7129f","Name":"blqw","Birthday":"\/Date(530964000000+0800)\/","Sex":"Male","IsDeleted":false,"LoginHistory":["\/Date(1377129600000+0800)\/","\/Date(1377137410000+0800)\/","\/Date(1377146036000+0800)\/","\/Date(1377163518000+0800)\/","\/Date(1377184019000+0800)\/"],"Info":{"Address":"廣東省廣州市","Phone":{"手機":"18688888888","電話":"82580000","短號":"10086","QQ":"21979018"},"ZipCode":510000},"Double":-Infinity}
ps:依然不支援迴圈引用,丟擲堆疊異常
測試結果(單位 "毫秒")
迴圈次數 | 測試元件 | 第一輪 | 第二輪 | 第三輪 | 第四輪 | 第五輪 |
1次 | JavaScriptSerializer | 12 | 0 | 0 | 0 | 0 |
Newtonsoft.Json | 208 | 0 | 0 | 0 | 0 | |
Jayrock.Json | 85 | 0 | 0 | 0 | 0 | |
fastJSON.NET | 47 | 0 | 0 | 0 | 0 | |
QuickJsonBuilder | 37 | 0 | 0 | 0 | 0 | |
JsonBuilder | 22 | 0 | 0 | 0 | 0 | |
ServiceStack.Text | 138 | 0 | 0 | 0 | 0 | |
100次 | JavaScriptSerializer | 23 | 7 | 7 | 8 | 7 |
Newtonsoft.Json | 201 | 2 | 3 | 2 | 2 | |
Jayrock.Json | 77 | 8 | 9 | 9 | 8 | |
fastJSON.NET | 41 | 1 | 1 | 1 | 1 | |
QuickJsonBuilder | 36 | 1 | 1 | 1 | 1 | |
JsonBuilder | 26 | 3 | 4 | 3 | 3 | |
ServiceStack.Text | 139 | 2 | 2 | 2 | 2 | |
10000次 | JavaScriptSerializer | 765 | 751 | 752 | 751 | 749 |
Newtonsoft.Json | 437 | 253 | 251 | 248 | 243 | |
Jayrock.Json | 967 | 905 | 965 | 913 | 952 | |
fastJSON.NET | 239 | 181 | 200 | 167 | 166 | |
QuickJsonBuilder | 171 | 128 | 132 | 136 | 129 | |
JsonBuilder | 418 | 386 | 388 | 391 | 360 | |
ServiceStack.Text | 367 | 216 | 224 | 238 | 223 |
測試結果僅供參考
總結
可以看出基本所有元件在第一輪用時都是最長的,應該是在處理一些初始化和快取的工作。
所以呢,我特別給出了1次的迴圈結果,
JavaScriptSerializer 可以看出在100次以內的時候微軟的還是有一些優勢的,隨著次數增加效能就漸漸跟不上了
Newtonsoft.Json一直表現平平啊,沒有特別突出就地方,就跟JsonBuilder比效能也沒有好到那裡去
Jayrock.Json 是昨天一位博友告訴我的,據說是淘寶sdk在用的,效能只能說,好差。。。
fastJSON.NET 確實是所有元件中最快的,QuickJsonBuilder也從中借鑑了一些處理方式
ServiceStack.Text 中規中矩吧比Newtonsoft.Json好一點點.就是我只找到4.0的拓展方法,不知道2.0下有沒有相應的元件
好吧 我自己的元件我就不做評價了
« 上一篇: 一種簡單,輕量,靈活的C#物件轉Json物件的方案
» 下一篇: 一種簡單,輕量,靈活的C#物件轉Json物件的方案(續)
上一篇文章中我已經介紹了JsonBuilder方案的整體思路以及一個版本的雛形程式碼,他現在已經是可以使用的了,但是因為是實時反射的,所以效率並不高。
鑑於幾位博友對Json轉換元件的效能有興趣,我先放幾個常用元件的測試結果以供參考。下一篇在詳細介紹JsonBuilder的優化工作
測試用例
測試用例 測試程式碼測試方案1:微軟自帶的的 JavaScriptSerializer
引用元件:System.Web.Extensions(微軟自帶了,引用下就好)
string str = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(User.TestUser()); //{"Self":null,"UID":"6f50b429-5c13-4875-a29a-e4bd8d7b2772","Name":"blqw","Birthday":"\/Date(530964000000)\/","Sex":0,"IsDeleted":false,"LoginHistory":["\/Date(1377129600000)\/","\/Date(1377137410000)\/","\/Date(1377146036000)\/","\/Date(1377163518000)\/","\/Date(1377184019000)\/"],"Info":{"Address":"廣東省廣州市","Phone":{"手機":"18688888888","電話":"82580000","短號":"10086","QQ":"21979018"},"ZipCode":510000},"Double":-Infinity}
測試方案2:Newtonsoft.Json
引用元件:Newtonsoft.Json.Net35.dll
string str = Newtonsoft.Json.JsonConvert.SerializeObject(User.TestUser()); //{"Self":null,"UID":"6f50b429-5c13-4875-a29a-e4bd8d7b2772","Name":"blqw","Birthday":"\/Date(530964000000+0800)\/","Sex":0,"IsDeleted":false,"LoginHistory":["\/Date(1377129600000+0800)\/","\/Date(1377137410000+0800)\/","\/Date(1377146036000+0800)\/","\/Date(1377163518000+0800)\/","\/Date(1377184019000+0800)\/"],"Info":{"Address":"廣東省廣州市","Phone":{"手機":"18688888888","電話":"82580000","短號":"10086","QQ":"21979018"},"ZipCode":510000},"Double":-Infinity}
測試方案3:Jayrock.Json
引用元件:Jayrock.Json.dll
JsonTextWriter jtw = new Jayrock.Json.JsonTextWriter(); Jayrock.Json.Conversion.JsonConvert.Export(User.TestUser(), jtw); string str = jtw.ToString(); //{"uID":"6f50b429-5c13-4875-a29a-e4bd8d7b2772","name":"blqw","birthday":"1986-10-29T18:00:00.0000000+08:00","sex":"Male","isDeleted":false,"loginHistory":["2013-08-22T08:00:00.0000000+08:00","2013-08-22T10:10:10.0000000+08:00","2013-08-22T12:33:56.0000000+08:00","2013-08-22T17:25:18.0000000+08:00","2013-08-22T23:06:59.0000000+08:00"],"info":{"address":"廣東省廣州市","phone":{"手機":"18688888888","電話":"82580000","短號":"10086","QQ":"21979018"},"zipCode":510000},"double":-Infinity}
測試方案4:fastJSON.NET
引用元件:fastJSON.dll
JSONParameters p = new JSONParameters(); p.EnableAnonymousTypes = p.IgnoreCaseOnDeserialize = p.ShowReadOnlyProperties = p.UseEscapedUnicode = p.UseExtensions = p.UseFastGuid = p.UseOptimizedDatasetSchema = p.UseUTCDateTime = p.UsingGlobalTypes = false;//所有引數為false,執行速度最快 string str = fastJSON.JSON.Instance.ToJSON(User.TestUser(), p); //{"Self":null,"UID":"6f50b429-5c13-4875-a29a-e4bd8d7b2772","Name":"blqw","Birthday":"1986-10-29 18:00:00","Sex":"Male","IsDeleted":false,"LoginHistory":["2013-08-22 08:00:00","2013-08-22 10:10:10","2013-08-22 12:33:56","2013-08-22 17:25:18","2013-08-22 23:06:59"],"Info":{"Address":"廣東省廣州市","Phone":{"手機":"18688888888","電話":"82580000","短號":"10086","QQ":"21979018"},"ZipCode":510000},"Double":-Infinity}
測試方案5,6:QuickJsonBuilder,JsonBuilder
ps:以上幾種型別都不支援迴圈引用,如果有,直接丟擲異常
說明:QuickJsonBuilder就是我上篇中介紹的JsonBuilder的終極形態
QuickJsonBuilder jb = new QuickJsonBuilder(); jb.ToJson(User.TestUser());
//{"Self":null,"UID":"6f50b429-5c13-4875-a29a-e4bd8d7b2772","Name":"blqw","Birthday":"1986-10-29 18:00:00","Sex":"Male","IsDeleted":false,"LoginHistory":["2013-08-22 08:00:00","2013-08-22 10:10:10","2013-08-22 12:33:56","2013-08-22 17:25:18","2013-08-22 23:06:59"],"Info":{"Address":"廣東省廣州市","Phone":{"手機":"18688888888","電話":"82580000","短號":"10086","QQ":"21979018"},"ZipCode":510000},"Double":-Infinity}
ps:當Self指向自己本身的時候,將返回 "Self":undefined 用以區分空物件
測試方案7:ServiceStack.Text
引用元件:ServiceStack.Text.dll 抱歉我只找到4.0的
這個是後來補上的,測試程式碼中沒有這個...
User.TestUser().ToJson(); //{"UID":"48d4624ad0c8475094d00fc769c7129f","Name":"blqw","Birthday":"\/Date(530964000000+0800)\/","Sex":"Male","IsDeleted":false,"LoginHistory":["\/Date(1377129600000+0800)\/","\/Date(1377137410000+0800)\/","\/Date(1377146036000+0800)\/","\/Date(1377163518000+0800)\/","\/Date(1377184019000+0800)\/"],"Info":{"Address":"廣東省廣州市","Phone":{"手機":"18688888888","電話":"82580000","短號":"10086","QQ":"21979018"},"ZipCode":510000},"Double":-Infinity}
ps:依然不支援迴圈引用,丟擲堆疊異常
測試結果(單位 "毫秒")
迴圈次數 | 測試元件 | 第一輪 | 第二輪 | 第三輪 | 第四輪 | 第五輪 |
1次 | JavaScriptSerializer | 12 | 0 | 0 | 0 | 0 |
Newtonsoft.Json | 208 | 0 | 0 | 0 | 0 | |
Jayrock.Json | 85 | 0 | 0 | 0 | 0 | |
fastJSON.NET | 47 | 0 | 0 | 0 | 0 | |
QuickJsonBuilder | 37 | 0 | 0 | 0 | 0 | |
JsonBuilder | 22 | 0 | 0 | 0 | 0 | |
ServiceStack.Text | 138 | 0 | 0 | 0 | 0 | |
100次 | JavaScriptSerializer | 23 | 7 | 7 | 8 | 7 |
Newtonsoft.Json | 201 | 2 | 3 | 2 | 2 | |
Jayrock.Json | 77 | 8 | 9 | 9 | 8 | |
fastJSON.NET | 41 | 1 | 1 | 1 | 1 | |
QuickJsonBuilder | 36 | 1 | 1 | 1 | 1 | |
JsonBuilder | 26 | 3 | 4 | 3 | 3 | |
ServiceStack.Text | 139 | 2 | 2 | 2 | 2 | |
10000次 | JavaScriptSerializer | 765 | 751 | 752 | 751 | 749 |
Newtonsoft.Json | 437 | 253 | 251 | 248 | 243 | |
Jayrock.Json | 967 | 905 | 965 | 913 | 952 | |
fastJSON.NET | 239 | 181 | 200 | 167 | 166 | |
QuickJsonBuilder | 171 | 128 | 132 | 136 | 129 | |
JsonBuilder | 418 | 386 | 388 | 391 | 360 | |
ServiceStack.Text | 367 | 216 | 224 | 238 | 223 |
測試結果僅供參考
總結
可以看出基本所有元件在第一輪用時都是最長的,應該是在處理一些初始化和快取的工作。
所以呢,我特別給出了1次的迴圈結果,
JavaScriptSerializer 可以看出在100次以內的時候微軟的還是有一些優勢的,隨著次數增加效能就漸漸跟不上了
Newtonsoft.Json一直表現平平啊,沒有特別突出就地方,就跟JsonBuilder比效能也沒有好到那裡去
Jayrock.Json 是昨天一位博友告訴我的,據說是淘寶sdk在用的,效能只能說,好差。。。
fastJSON.NET 確實是所有元件中最快的,QuickJsonBuilder也從中借鑑了一些處理方式
ServiceStack.Text 中規中矩吧比Newtonsoft.Json好一點點.就是我只找到4.0的拓展方法,不知道2.0下有沒有相應的元件
好吧 我自己的元件我就不做評價了