.net下两种json序列化速度比对(已添加源代码下载地址)
2008-11-24 17:01
295 查看
最近研究JSON,想把datatable直接序列化为json,上网查了查,发现.net下有这么两种json序列化的方法:
一是用Newtonsoft.Json.dll进行json序列化,这个从json的官网上就可以找到,使用比较方便。
二是用xml-2-json.xsl这个xslt模板转换,详细请看这里http://blog.csdn.net/isaaq/archive/2006/12/09/1436357.aspx
两种序列话方法都可以达到要求,但是我想看一下两种分别的速度如何,于是自己写个程序试一下。
生成datatable的代码如下
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
private DataTable getDataTable(string name)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
DataTable dt = new DataTable(name);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
dt.Columns.Add(new DataColumn("name", typeof(string)));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
dt.Columns.Add(new DataColumn("password", typeof(string)));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
DataRow dr;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for (int i = 0; i < 100; i++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
dr = dt.NewRow();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
dr[0] = i.ToString();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
dr[1] = "P" + i.ToString();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
dt.Rows.Add(dr);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return dt;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
其中循环次数分别是10,100,1000,1000,然后看一下两种方法的序列话速度。结果见下表
程序写的并不严谨,只是简单比对一下速度而已,数据都是多次运行取的相当稳定的值而已,并不能说明具体时间。有一个奇怪的现象是,循环次数为10000时,用XSLT转换一般都是30秒左右,但有时会有10秒左右的现象出现,不知是什么原因。
有需要源代码的朋友请留下邮箱,我会尽快发过去的。另外想请大家帮忙看一下我用json.dll序列化datatable的方法是否正确,我是把datatable转换为xml,然后用json.dll转换xml为json,不知道有没有更好的方法,谢谢大家。
源代码已经上传到http://download.csdn.net/user/luq885/,请自行下载
一是用Newtonsoft.Json.dll进行json序列化,这个从json的官网上就可以找到,使用比较方便。
二是用xml-2-json.xsl这个xslt模板转换,详细请看这里http://blog.csdn.net/isaaq/archive/2006/12/09/1436357.aspx
两种序列话方法都可以达到要求,但是我想看一下两种分别的速度如何,于是自己写个程序试一下。
生成datatable的代码如下
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
private DataTable getDataTable(string name)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
DataTable dt = new DataTable(name);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
dt.Columns.Add(new DataColumn("name", typeof(string)));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
dt.Columns.Add(new DataColumn("password", typeof(string)));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
DataRow dr;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for (int i = 0; i < 100; i++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
dr = dt.NewRow();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
dr[0] = i.ToString();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
dr[1] = "P" + i.ToString();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
dt.Rows.Add(dr);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return dt;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
其中循环次数分别是10,100,1000,1000,然后看一下两种方法的序列话速度。结果见下表
JSON.DLL | XSLT | |
10 | 0毫秒 | 31.25毫秒 |
100 | 15.625毫秒 | 31.25毫秒 |
1000 | 46.875毫秒 | 140.625毫秒 |
10000 | 9531.25毫秒 | 31218.75毫秒 |
有需要源代码的朋友请留下邮箱,我会尽快发过去的。另外想请大家帮忙看一下我用json.dll序列化datatable的方法是否正确,我是把datatable转换为xml,然后用json.dll转换xml为json,不知道有没有更好的方法,谢谢大家。
源代码已经上传到http://download.csdn.net/user/luq885/,请自行下载
相关文章推荐
- .net下两种json序列化速度比对(已添加源代码下载地址)
- .net下两种json序列化速度比对
- Net网络通讯编程[利用Socket实现字串、文件、序列化对象传输]--前面6篇博文全部源代码下载地址
- Net网络通讯编程[利用Socket实现字串、文件、序列化对象传输]--前面6篇博文全部源代码下载地址
- 在ASP.net 3.5中 用JSON序列化对象(两种方法)
- Json.net|NH|Log4net|Test等工具下载地址
- 转载:在ASP.net 3.5中 用JSON序列化对象(两种方法)
- 黑马程序员_学习日记73_725ASP.NET(禁用Cookie、ASP.NET中的重要对象、JavaScript实现AJAX、两种Json格式的序列化方法、JQuery中实现ajax的五种方法)
- asp.net中序列化和反序列化json的两种常用方式
- struts1、struts2所有版本源代码下载地址大全,速度围观
- wp7天气预报源代码(二序列化谷歌数据)公布源代码下载地址
- Json.net|NH|Log4net|Test等工具下载地址
- wp7天气预报源代码(二序列化谷歌数据)公布源代码下载地址
- struts1、struts2所有版本源代码下载地址大全,速度围观
- ASP.NET经典源代码下载地址及数据库配置方法
- 在ASP.net 3.5中 用JSON序列化对象(两种方法)(转自LeonSky)
- struts1、struts2所有版本源代码下载地址大全,速度围观
- ASP.NET经典源代码下载地址及数据库配置方法
- json.net 序列化组件 Newtonsoft.Json 如果有不需要序列化的字段,可以给该字段添加[JsonIgnore]标记
- 步步为营:Asp.Net序列化Json格式的两种方法