.NET ez 2013-08-27
本篇測試 JSON TO String 及 String TO JSON,使用以下三種方式 JavaScriptSerializer、Json.NET、fastJSON 進行效能測試 。
測試原始碼下載:fastJsonDemo
原始碼如下:
using System;
using System.Diagnostics;
using System.Web.Script.Serialization;
using fastJSON;
using Newtonsoft.Json;
namespace fastJsonDemo
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
testSpeed();
Write("<BR>");
testConvertDatetime();
}
protected void testSpeed() {
int i_testCount = 30000;
User[] l_users = new User[i_testCount];
for (int i = 0; i < i_testCount; i++) l_users[i] = new User { ID = i, Name = "TEST", Age = 20 + i, Address = "100 臺北市..." + i.ToString() + "號" };
/* 使用 JavaScriptSerializer */
testJavaScriptSerializer(l_users);
/* 使用 Json.NET */
testJsonDotNET(l_users);
/* 使用 fastJSON */
testfastJSON(l_users);
}
protected void testJavaScriptSerializer(User[] l_users)
{
string s_jsonstr = string.Empty;
Stopwatch sw = new Stopwatch();
Write("<BR>使用 JavaScriptSerializer 物件 to JSON<BR>", "#524CE4", true);
sw.Reset();
sw.Start();
s_jsonstr = ConvertObject2JSON(l_users);
sw.Stop();
Write(sw.Elapsed.Milliseconds.ToString() + " ms<BR>", "#666", true);
Write("使用 JavaScriptSerializer JSON to 物件<BR>", "#524CE4", true);
sw.Reset();
sw.Start();
User[] l_tempusers = (User[])ConvertJSON2Object(s_jsonstr, typeof(User[]));
sw.Stop();
Write(sw.Elapsed.Milliseconds.ToString() + " ms<BR>", "#666", true);
Write(ConvertObject2JSON(new User[] { l_users[0], l_users[1], l_users[2] }) + "<BR>", "#F74B14", false);
}
protected void testJsonDotNET(User[] l_users)
{
string jsonstr = string.Empty;
Stopwatch sw = new Stopwatch();
Write("<BR>使用 Json.NET 物件 to JSON<BR>", "#524CE4", true);
sw.Reset();
sw.Start();
jsonstr = JsonConvert.SerializeObject(l_users);
sw.Stop();
Write(sw.Elapsed.Milliseconds.ToString() + " ms<BR>", "#666", true);
Write("使用 Json.NET JSON to 物件<BR>", "#524CE4", true);
sw.Reset();
sw.Start();
User[] l_tempusers = JsonConvert.DeserializeObject<User[]>(jsonstr);
sw.Stop();
Write(sw.Elapsed.Milliseconds.ToString() + " ms<BR>", "#666", true);
Write(JsonConvert.SerializeObject(new User[] { l_users[0], l_users[1], l_users[2] }) + "<BR>", "#F74B14", false);
}
protected void testfastJSON(User[] l_users)
{
string s_jsonstr = string.Empty;
Stopwatch sw = new Stopwatch();
Write("<BR>使用 fastJSON 物件 to JSON<BR>", "#524CE4", true);
sw.Reset();
sw.Start();
s_jsonstr = JSON.Instance.ToJSON(l_users);
sw.Stop();
Write(sw.Elapsed.Milliseconds.ToString() + " ms<BR>", "#666", true);
Write("使用 fastJSON JSON to 物件<BR>", "#524CE4", true);
sw.Reset();
sw.Start();
User[] l_tempusers = fastJSON.JSON.Instance.ToObject(s_jsonstr) as User[];
sw.Stop();
Write(sw.Elapsed.Milliseconds.ToString() + " ms<BR>", "#666", true);
Write(JSON.Instance.ToJSON(new User[] { l_users[0], l_users[1], l_users[2] }) + "<BR>", "#F74B14", false);
}
protected void testConvertDatetime() {
string s_js = string.Empty;
DateTime dt = DateTime.Now;
Write("<BR>使用 JavaScriptSerializer <BR>", "#8F3535", true);
s_js = ConvertObject2JSON(dt);
Write(s_js + " <BR>", "#666", true);
Write(((DateTime)ConvertJSON2Object(s_js, typeof(DateTime))).ToString() + " <BR>", "#666", true);
Write("<BR>使用 Json.NET <BR>", "#8F3535", true);
s_js = JsonConvert.SerializeObject(dt);
Write(s_js +"<BR>", "#666", true);
Write(JsonConvert.DeserializeObject<DateTime>(s_js).ToString() + "<BR>", "#666", true);
Write("<BR>使用 fastJSON <BR>", "#8F3535", true);
s_js = JSON.Instance.ToJSON(dt);
Write(s_js + " <BR>", "#666", true);
Write(DateTime.Parse(JSON.Instance.ToObject(s_js).ToString()).ToString() + " <BR>", "#666", true);
}
protected string ConvertObject2JSON(object Source)
{
JavaScriptSerializer JSONSerializer = new JavaScriptSerializer();
JSONSerializer.RecursionLimit = Int32.MaxValue;
return JSONSerializer.Serialize(Source);
}
protected object ConvertJSON2Object(string JSON, Type ObjectType)
{
JavaScriptSerializer JSONSerializer = new JavaScriptSerializer();
JSONSerializer.RecursionLimit = Int32.MaxValue;
return JSONSerializer.Deserialize(JSON, ObjectType);
}
protected void Write(string word, string color, bool bord)
{
if (bord)
Response.Write("<b><font color=\"" + color + "\">" + word + "</font></b>");
else
Response.Write("<font color=\"" + color + "\">" + word + "</font>");
}
protected void Write(string word)
{
Response.Write(word);
}
}
public class User
{
public long ID { get; set; }
public string Name { get; set; }
public int Age { get; set; }
public string Address { get; set; }
}
}測試結果如下圖:
可以發現 fastJSON 速度領先另外兩個方法,但是字串較長!如果頻寬或儲存空間不足,可能使用 Json.NET 會比較好,最差的就是 JavaScriptSerializer 所以個人建議不要使用!
標籤: .NET
