上一篇文章中我已经介绍了JsonBuilder方案的整体思路以及一个版本的雏形代码,他现在已经是可以使用的了,但是因为是实时反射的,所以效率并不高。
  鉴于几位博友对Json转换组件的性能有兴趣,我先放几个常用组件的测试结果以供参考。下一篇在详细介绍JsonBuilder的优化工作
  测试用例
using System;
using System.Collections.Generic;
/// <summary> 用户对象
/// </summary>
public class User
{
public static User TestUser()
{//这里我尽量构造一个看上去很复杂的对象,并且这个对象几乎涵盖了所有常用的类型
User user = new User();
user.UID = Guid.NewGuid();
user.Birthday = new DateTime(1986, 10, 29, 18, 00, 00);
user.IsDeleted = false;
user.Name = "blqw";
user.Sex = UserSex.Male;
user.LoginHistory = new List<DateTime>();
user.LoginHistory.Add(DateTime.Today.Add(new TimeSpan(8, 00, 00)));
user.LoginHistory.Add(DateTime.Today.Add(new TimeSpan(10, 10, 10)));
user.LoginHistory.Add(DateTime.Today.Add(new TimeSpan(12, 33, 56)));
user.LoginHistory.Add(DateTime.Today.Add(new TimeSpan(17, 25, 18)));
user.LoginHistory.Add(DateTime.Today.Add(new TimeSpan(23, 06, 59)));
user.Info = new UserInfo();
user.Info.Address = "广东省广州市";
user.Info.ZipCode = 510000;
user.Info.Phone = new Dictionary<string, string>();
user.Info.Phone.Add("手机", "18688888888");
user.Info.Phone.Add("电话", "82580000");
user.Info.Phone.Add("短号", "10086");
user.Info.Phone.Add("QQ", "21979018");
user.Double = Double.NegativeInfinity;
// user.Self = user; //这里是用来测试循环引用的解析情况
return user;
}
public User Self { get; set; }
//User self
/// <summary> ID
/// </summary>
public Guid UID { get; set; }
/// <summary> 用户名称
/// </summary>
public string Name { get; set; }
/// <summary> 生日
/// </summary>
public DateTime Birthday { get; set; }
/// <summary> 性别
/// </summary>
public UserSex Sex { get; set; }
/// <summary> 是否删除标记
/// </summary>
public bool IsDeleted { get; set; }
/// <summary> 近登录记录
/// </summary>
public List<DateTime> LoginHistory { get; set; }
/// <summary> 联系信息
/// </summary>
public UserInfo Info { get; set; }
public Double Double { get; set; }
}
/// <summary> 用户性别
/// </summary>
public enum UserSex
{
/// <summary> 男
/// </summary>
Male,
/// <summary> 女
/// </summary>
Female
}
/// <summary> 用户信息
/// </summary>
public class UserInfo
{
/// <summary> 地址
/// </summary>
public string Address { get; set; }
/// <summary> 联系方式
/// </summary>
public Dictionary<string, string> Phone { get; set; }
/// <summary> 邮政编码
/// </summary>
public int ZipCode { get; set; }
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.Reflection.Emit;
using System.Data;
using fastJSON;
using System.IO;
using System.Threading;
namespace blqw
{
class Program
{
static void Main(string[] args)
{
int count = 10000;
var obj = User.TestUser();
TestJavaScriptSerializer(count, obj);
TestNewtonsoftJson(count, obj);
TestJayrockJson(count, obj);
TestFastJson(count, obj);
TestQuickJsonBuilder(count, obj);
TestJsonBuilder(count, obj);
}
//测试QuickJsonBuilder性能
static void TestQuickJsonBuilder(int count, object obj)
{
Stopwatch sw = new Stopwatch();
Console.WriteLine();
Console.WriteLine("QuickJsonBuilder 每次" + count + " 共10次");
for (int j = 0; j < 10; j++)
{
sw.Restart();
for (int i = 0; i < count; i++)
{
QuickJsonBuilder jb = new QuickJsonBuilder();
jb.ToJson(obj);
}
sw.Stop();
Console.Write(sw.ElapsedMilliseconds + "ms | ");
}
Console.WriteLine();
Console.WriteLine("结果:" + new QuickJsonBuilder().ToJson(obj));
Console.WriteLine("==============================================");
}