haoxiaobo

从C到C++又到.net, 有一些心得, 和大家交流下...
随笔 - 64, 文章 - 6, 评论 - 635, 阅读 - 18万
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

提醒一下:XmlSerializer的效率比BinaryFormatter高!

Posted on   HAL9000  阅读(1899)  评论(6编辑  收藏  举报
从前的经验是:二进制文件的读写效率比文本的高,不要说还要解析xml结构啥的。

于是,前几天需要暂存内存里百万条左右的数据时,毫不迟疑地选择了BinaryFormatter.

每次重新读回内存都要N长时间。

最后一次,为了方便人工查看,改了一下用了 XmlSerializer来保存数据,人工查看处理完之后,再加载到内存里,突然感觉怎么这么快!

 

于是找时间做了个测试,发现自己杯具了,知识更新太慢,不知道原来xml的效率已经超过bin了!

 

下面是对比:

 

XmlSerializer

BinaryFormatter

59,031 ms

117,763 ms

61,925 ms

64,565 ms

文件容量

794,067,056 byte

314,505,339 byte

 但是文件大小xml是bin的2.5倍。 但是我看了生成的文件内容,确认binaryformatter并没有采用压缩算法。

即然硬盘空间不是问题,以后就xml吧。

 ---------------------------------------------------

测试程序很简单:

xml保存: 

 xml保存


xml加载数据 :

复制代码
xml加载数据
XmlSerializer formatter = new XmlSerializer(typeof(ArrayList),
new Type[]
{
 
typeof(App_risk), typeof(Appnext), typeof(Chagapp),
 
typeof(Chgcon), typeof(Ch_apid), typeof(Ch_enno),
 
typeof(Ch_handno), typeof(Ch_id), typeof(Ch_insno),
 
typeof(Ch_pono), typeof(Ch_p_handno), typeof(Ch_txhand),
 
typeof(Comcust), typeof(Custmatl), typeof(Hxbtest),
 
typeof(Modify), typeof(Nclmccl), typeof(Nmodclaim),
 
typeof(Nregclm), typeof(Prerec), typeof(Relpayrc),
 
typeof(Riskcon), typeof(Tmprec), typeof(Riskspec)
}
);
ArrayList al 
= (ArrayList)formatter.Deserialize(fs);
复制代码

 

bin保存数据: 

BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(fs, (ArrayList)
this.htTargetTables[sTabName]);

 

BIN加载数据:
BinaryFormatter formatter = new BinaryFormatter();
ArrayList al 
= (ArrayList)formatter.Deserialize(fs);

 

 

 

 

编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
点击右上角即可分享
微信分享提示