-
C#教程之C#教程之[.NET]使用十年股价对比各种序列化技术(2)
本站最新发布 C#从入门到精通
试听地址 https://www.xin3721.com/eschool/CSharpxin3721/
)) { bytes = new byte[sizeof(int)]; stream.Read(bytes, 0, bytes.Length); value = BitConverter.ToInt32(bytes, 0); } else if (property.PropertyType == typeof(short)) { bytes = new byte[sizeof(short)]; stream.Read(bytes, 0, bytes.Length); value = BitConverter.ToInt16(bytes, 0); } else if (property.PropertyType == typeof(float)) { bytes = new byte[sizeof(float)]; stream.Read(bytes, 0, bytes.Length); value = BitConverter.ToSingle(bytes, 0); } else if (property.PropertyType == typeof(double)) { bytes = new byte[sizeof(double)]; stream.Read(bytes, 0, bytes.Length); value = BitConverter.ToDouble(bytes, 0); } property.SetValue(price, value); index += bytes.Length; } result.Add(price); } return result; } }
试听地址 https://www.xin3721.com/eschool/CSharpxin3721/
)) { bytes = new byte[sizeof(int)]; stream.Read(bytes, 0, bytes.Length); value = BitConverter.ToInt32(bytes, 0); } else if (property.PropertyType == typeof(short)) { bytes = new byte[sizeof(short)]; stream.Read(bytes, 0, bytes.Length); value = BitConverter.ToInt16(bytes, 0); } else if (property.PropertyType == typeof(float)) { bytes = new byte[sizeof(float)]; stream.Read(bytes, 0, bytes.Length); value = BitConverter.ToSingle(bytes, 0); } else if (property.PropertyType == typeof(double)) { bytes = new byte[sizeof(double)]; stream.Read(bytes, 0, bytes.Length); value = BitConverter.ToDouble(bytes, 0); } property.SetValue(price, value); index += bytes.Length; } result.Add(price); } return result; } }
Name | Serialize(ms) | Deserialize(ms) | Bytes |
---|---|---|---|
ReflectionSerializer | 413 | 431 | 103,246 |
好像好了一些,但性能大幅下降。我好像记得有人说过.NET会将反射缓存让我不必担心反射带来的性能问题,看来我的理解有出入。索性自己缓存些反射结果:
private readonly IEnumerable<PropertyInfo> _properties; public ExtendReflectionSerializer() { _properties = typeof(StockPriceSlim).GetProperties().Where(p => p.GetCustomAttribute(typeof(DataMemberAttribute)) != null).ToList(); }
Name | Serialize(ms) | Deserialize(ms) | Bytes |
---|---|---|---|
ExtendReflectionSerializer | 11 | 11 | 103,246 |
这样改进后性能还可以接受。
6. 最后试试压缩#
最后试试在序列化的基础上再随便压缩一下:
public byte[] SerializeWithZip(List<StockPriceSlim> instance) { var bytes = SerializeSlim(instance); using (var memoryStream = new MemoryStream()) { using (var deflateStream = new DeflateStream(memoryStream, CompressionLevel.Fastest)) { deflateStream.Write(bytes, 0, bytes.Length); } return memoryStream.ToArray(); } } public List<StockPriceSlim> DeserializeWithZip(byte[] source) { using (var originalFileStream = new MemoryStream(source)) { using (var memoryStream = new MemoryStream()) { using (var decompressionStream = new DeflateStream(originalFileStream, CompressionMode.Decompress)) { decompressionStream.CopyTo(memoryStream); } var bytes = memoryStream.ToArray(); return DeserializeSlim(bytes); } } }
结果看来不错:
Name | Serialize(ms) | Deserialize(ms) | Bytes | Serialize With Zip(ms) | Deserialize With Zip(ms) | Bytes With Zip |
---|---|---|---|---|---|---|
BinarySerializer | 11 | 12 | 141,930 | 22 | 12 | 72,954 |
XmlSerializer | 42 | 24 | 977,248 | 24 | 28 | 108,839 |
SoapSerializer | 48 | 89 | 2,586,720 | 61 | 87 | 140,391 |
JsonSerializer | 17 | 33 | 411,942 | 24 | 35 | 90,125 |
ProtobufSerializer | 7 | 3 | 130,416 | 7 | 6 | 65,644 |
CustomSerializer | 5 | 1 | 103,246 | 9 | 3 | 57,697 |
ReflectionSerializer | 413 | 431 | 103,246 | 401 | 376 | 59,285 |
ExtendReflectionSerializer | 11 | 11 | 103,246 | 13 | 14 | 59,285 |
7. 结语#
满足了好奇心,顺便复习了一下各种序列化的方式。
因为原来的需求就很单一,没有测试各种数据量下的对比。
虽然Protobuf十分优秀,但在本地存储序列化文件时为了可读性我通常都会选择XML或JSON。
8. 参考#
二进制序列化 XML 和 SOAP 序列化 Json.NET Protocol Buffers - Google's data interchange format
9. 源码#
StockDataSample
作者:Dino.C
出处:https://www.cnblogs.com/dino623/p/Serialize.html
本站使用「署名 4.0 国际」创作共享协议,转载请在文章明显位置注明作者及出处。
栏目列表
最新更新
如何使用OS模块中的stat方法
Python os 模块
seek() 方法
python打开文件实例1
Python写入文件
什么是流?
文件操作如何进制逐行读取
Python相对路径
with创建临时运行环境
Python文件操作
.Net Standard(.Net Core)实现获取配置信息
Linux PXE + Kickstart 自动装机
Shell 编程 基础
Shell 编程 条件语句
CentOS8-网卡配置及详解
Linux中LVM逻辑卷管理
1.数码相框-相框框架分析(1)
Ubuntu armhf 版本国内源
Linux中raid磁盘阵列
搭建简易网站
access教程之Access简介
mysql 安装了最新版本8.x版本后的报错:
Mysql空间数据&空间索引(spatial)
如何远程连接SQL Server数据库的图文教程
复制SqlServer数据库的方法
搜索sql语句
sql中返回参数的值
sql中生成查询的模糊匹配字符串
数据定义功能
数据操作功能