Newtonsoft.json中 linq to json 和序列化哪个快?
Newtonsoft.json是最常用的json序列化组件,当然他不是最快的,但是是功能最全的。。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
namespace ConsoleApplication2
{
public class Entity
{
public int a;
public bool b = false;
public string s = "asdfgb";
public double d = 123.456;
}
class Program
{
static void Main(string[] args)
{
Stopwatch w = new Stopwatch();
w.Start();
for (int i = 0; i <= 1000; i++)
{
Entity a = new Entity();
a.a = i;
string result = new JObject(new JProperty("a", i),
new JProperty("b", a.b),
new JProperty("s", a.s),
new JProperty("d", a.d)
).ToString();
}
w.Stop();
Console.WriteLine(w.ElapsedMilliseconds);
w.Reset();
w.Start();
for (int i = 0; i <= 1000; i++)
{
try
{
//Entity a = new Entity();
//a.a = i;
string result = JsonConvert.SerializeObject(new
{
a = i,
b = false,
s = "asdfgb",
d = 123.456
});
}
catch (Exception e)
{
}
}
w.Stop();
Console.WriteLine(w.ElapsedMilliseconds);
w.Reset();
w.Start();
for (int i = 0; i <= 1000; i++)
{
try
{
//Entity a = new Entity();
//a.a = i;
string result = string.Format("{a: {0},b:{1},s:{2}},d:{3}",
i, false, "asdfgb", 123.456);
}
catch (Exception e)
{
}
}
w.Stop();
Console.WriteLine(w.ElapsedMilliseconds);
Console.Read();
}
}
}
最终结果整理:
方式: linq 序列化 字符串
100: 28 104 11
1000: 32 108 41
10000: 69 126 337
100000: 425 318 3341
随着次数的增加,估计反射的缓存还是很快的,序列化速度超过了linq to json,拼字符串从最快的变成最慢的
Newtonsoft.json中 linq to json 和序列化哪个快?的更多相关文章
- JSON中JObject和JArray,JValue序列化(Linq)
http://blog.csdn.net/lovegonghui/article/details/50293629 一.JObject和JArray序列化 1.实例化JArray和JObject,然后 ...
- Visual Studio 2017 - Windows应用程序打包成exe文件(2)- Advanced Installer 关于Newtonsoft.Json,LINQ to JSON的一个小demo mysql循环插入数据、生成随机数及CONCAT函数 .NET记录-获取外网IP以及判断该IP是属于网通还是电信 Guid的生成和数据修整(去除空格和小写字符)
Visual Studio 2017 - Windows应用程序打包成exe文件(2)- Advanced Installer Advanced Installer :Free for 30 da ...
- Json.Net 中Linq to JSON的操作
Linq to JSON是用来操作JSON对象的.可以用于快速查询,修改和创建JSON对象.当JSON对象内容比较复杂,而我们仅仅需要其中的一小部分数据时,可以考虑使用Linq to JSON来读取和 ...
- [Json.net]Linq to Json
引言 上篇学习了json.net的基本知识,这篇学习linq to json. 上篇文章:[Json.net]快速入门 Linq to Json Linq to Json是用来快速操作json对象的, ...
- 关于Newtonsoft.Json,LINQ to JSON的一个小demo
nuget获取Newtonsoft.Json github地址:Newtonsoft.Json public static void Test1() { /* 文本格式如下 代码实现目的: 1.VR ...
- json中把非json格式的字符串转换成json对象再转换成json字符串
JSON.toJson(str).toString()假如key和value都是整数的时候,先转换成jsonObject对象,再转换成json字符串
- 在JS和.NET中使用JSON (以及使用Linq to JSON定制JSON数据)
转载原地址: http://www.cnblogs.com/mcgrady/archive/2013/06/08/3127781.html 阅读目录 JSON的两种结构 认识JSON字符串 在JS中如 ...
- c#代码 天气接口 一分钟搞懂你的博客为什么没人看 看完python这段爬虫代码,java流泪了c#沉默了 图片二进制转换与存入数据库相关 C#7.0--引用返回值和引用局部变量 JS直接调用C#后台方法(ajax调用) Linq To Json SqlServer 递归查询
天气预报的程序.程序并不难. 看到这个需求第一个想法就是只要找到合适天气预报接口一切都是小意思,说干就干,立马跟学生沟通价格. 不过谈报价的过程中,差点没让我一口老血喷键盘上,话说我们程序猿的人 ...
- Linq to json
Json.Net系列教程 4.Linq To JSON 一.Linq to JSON是用来干什么的? Linq to JSON是用来操作JSON对象的.可以用于快速查询,修改和创建JSON对象.当JS ...
随机推荐
- Xamarin笔记
Xamarin学习笔记 1. Xamarin Studio自动更新下载的安装文件缓存路径:C:\Users\登录用户\AppData\Local\XamarinStudio-5.0\Cache\Tem ...
- storm学习好文链接
大圆的那些事:http://www.cnblogs.com/panfeng412/tag/Storm/ xcc的博客:http://blog.csdn.net/damacheng/article/ca ...
- 【Linux】crontab 定时任务
7月份,公司海外运营国发生数据库联接未释放,造成连接池连接不足的情况, 当时查询并没有及时解决问题, 为了避免现场同事多次人工重启系统的,因此写了个shell脚本,通过crontab 实现系统重启,但 ...
- IOS管理文件和目录
1.常见的NSFileManager文件方法 -(NSData *)contentsAtPath:path //从一个文件读取数据 -(BOOL)createFileAtPath: path cont ...
- jsp 中 有没有类似java if else语句
<c:when test=""></c:when> <c:otherwise></c:otherwise> 有if else的功能 ...
- 一次sql排序的问题。
select date, count(fail) as fail,count(win) as win from (select date,(case (result) when 'fail' then ...
- DoTween 教程
官方网站:http://dotween.demigiant.com/ 下载地址:http://dotween.demigiant.com/download.php pro版下载地址:http://pa ...
- ubuntu 下emacs 配置
(set-language-environment 'Chinese-GB) (set-keyboard-coding-system 'utf-8) (set-clipboard-coding-sys ...
- 学习 React(jsx语法) + es2015 + babel + webpack
视频学习地址: http://www.jtthink.com/course/play/575 官方地址 https://facebook.github.io/react/ 神坑: 1.每次this.s ...
- Hbuilder开发HTML5 APP之图标和启动页制作
1.点击项目下的"manifest.json"文件,会出现自动化的配置工具: 2.点“图标配置“,上传制作好的图标文件,自动生成不同大小的ico,这个要赞下! 3.启动图片(spl ...