现在的位置: 首页 > 综合 > 正文

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

2012年05月23日 ⁄ 综合 ⁄ 共 1273字 ⁄ 字号 评论关闭
从前的经验是:二进制文件的读写效率比文本的高,不要说还要解析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);

 

 

 

 

抱歉!评论已关闭.