VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > 编程开发 > c#教程 >
  • 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; } }
Name Serialize(ms) Deserialize(ms) Bytes
ReflectionSerializer 413 431 103,246

好像好了一些,但性能大幅下降。我好像记得有人说过.NET会将反射缓存让我不必担心反射带来的性能问题,看来我的理解有出入。索性自己缓存些反射结果:


Copy
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. 最后试试压缩#

最后试试在序列化的基础上再随便压缩一下:


Copy
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 国际」创作共享协议,转载请在文章明显位置注明作者及出处。


相关教程
        
关于我们--广告服务--免责声明--本站帮助-友情链接--版权声明--联系我们       黑ICP备07002182号