• c# 序列化效率比拼


    前言:作为开发人员,对象的序列化经常用到,特别是在现在前后端分离 采用json 交互 ,就将原来用过的几种方式总结了下,也算是做一个记录,顺便做了下性能测试。

    1:内置 JavaScriptSerializer

    2:常用  Newtonsoft.Json.dll

    3:第3方 Jil  https://github.com/kevin-montrose/Jil

    using Jil;
    using Newtonsoft.Json;
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.IO;
    using System.Web.Script.Serialization;
    
    namespace csDemo
    {
        class Program
        {
            static void Main(string[] args)
            {
                var sp_script = new Stopwatch();
                //模拟数据源 默认10w
                var dataLsit = GetData();
    
                #region  JavaScriptSerializer 添加 System.Web.Extensions引用
                sp_script.Start();//开始计数
                var js = new JavaScriptSerializer
                {
                    MaxJsonLength = Int32.MaxValue  //设置为int的最大值 
                };
                js.Serialize(dataLsit);
                sp_script.Stop();
                Console.WriteLine("JavaScriptSerializer序列化方式序列化" + dataLsit.Count + "个对象耗时:" + sp_script.ElapsedMilliseconds + "毫秒");
                #endregion
    
                #region  Newtonsoft.Json.dll 
                sp_script.Restart();//停止时间间隔测量,将运行时间重置为零,然后开始测量运行时间。
                JsonConvert.SerializeObject(dataLsit); ;
                sp_script.Stop();
                Console.WriteLine("Newtonsoft 序列化方式序列化" + dataLsit.Count + "个对象耗时:" + sp_script.ElapsedMilliseconds + "毫秒");
                #endregion
    
                #region Jil 序列化 取出数据  output.ToString(); 
                sp_script.Restart();
                using (var output = new StringWriter())
                {
                     JSON.Serialize(dataLsit,output );
                   
                }
                sp_script.Stop();
                Console.WriteLine("Jil 序列化方式序列化" + dataLsit.Count + "个对象耗时:" + sp_script.ElapsedMilliseconds + "毫秒");
                #endregion
            }
    
            // 测试模拟数据源 length 默认参数10w 
            public static dynamic GetData(int length=100000)
            {
                var lstRes = new List<dynamic>();
                for (var i = 0; i < length; i++)
                {
                    var data = new 
                    {
                        Name = "张三" + i,
                        Age = 20,
                        IsChild = i % 5 == 0 ? true : false,
                        Test1 = DateTime.Now,
                        Test2 = i.ToString(),
                        Test3 = i.ToString(),
                        Test4 = i.ToString(),
                        Test5 = i.ToString(),
                        Test6 = i.ToString(),
                        Test7 = i.ToString(),
                        Test8 = i.ToString(),
                        Test9 = i.ToString(),
                        Test10 = i.ToString()
                    };
    
                    lstRes.Add(data);
                }
                return lstRes;
            }
        }
    

    总结:使用过程中发现GetData方法 使用dynamic (开始使用实体类) 效率反而快一点。 特别是 Jil 更明显。

    Jil 学习地址:

    https://github.com/kevin-montrose/Jil

    https://www.dotnetjalps.com/2015/10/convert-c-object-into-json-and-vice.html

    附加 webapi  :使用Jil提升Json序列化性能

    https://blog.csdn.net/sqqyq/article/details/51692342

     注意:jil 对序列化类型有要求:(如果是object 或者是dataset 不支持) 时间需要自定义 建议时间数据转换string 默认的格式 yyyy-MM-dd hh:mm:ss

    Supported Types

    Jil will only (de)serialize types that can be reasonably represented as JSON.

    The following types (and any user defined types composed of them) are supported:

    • Strings (including char)
    • Booleans
    • Integer numbers (int, long, byte, etc.)
    • Floating point numbers (float, double, and decimal)
    • DateTimes & DateTimeOffsets
      • Note that DateTimes are converted to UTC time to allow for round-tripping, use DateTimeOffsets if you need to preserve timezone information
      • See Configuration for further details
    • TimeSpans
      • See Configuration for further details
    • Nullable types
    • Enumerations
      • Including [Flags]
    • Guids
    • IList<T>, ICollection<T>, and IReadOnlyList<T> implementations
    • IDictionary<TKey, TValue> implementations where TKey is a string or enumeration
    • ISet<T>

    额外笔记:

    css 学习网址 :https://www.zhangxinxu.com/wordpress/2012/09/css3-3d-transform-perspective-animate-transition/

    js:http://www.ruanyifeng.com/blog/archives.html

    工作记录:

    页面导入exel 建议使用 js-xlsx 

     )  提交到后台在逻辑判断  。2次请求改成一次请求,减少中间过程序列化反序列。

     js-xlsx:https://www.cnblogs.com/liuxianan/p/js-excel.html

  • 相关阅读:
    C# 枚举、字符串、值的相互转换
    What's New in v2010 vol 2.5
    Using Oracle's Parallel Execution Features
    [zhuan]asp.net程序性能优化的七个方面 (c#(或vb.net)程序改进)
    ORACLE常用网址
    html中的块元素(block element)和内联元素(inline element)
    软件构架师的特点
    窗体信息处理函数讲解
    [xue]软件项目经理所必需具备的素质
    Gulp系列文章入门Gulp
  • 原文地址:https://www.cnblogs.com/y112102/p/10232106.html
Copyright © 2020-2023  润新知