怎么样将大数据量DataSet进行压缩?
(声明:魏滔序原创,转贴请注明出处。)对于在WebService上必须(无法避免的情况下)要传输大量数据的DataSet时,对DataSet压缩是最好的方法,这样可以减少大量在网络传输过程中所占用的时间。
下面对我掌握的的DataSet压缩方法做一个测试,希望高人有斧正之处,也希望需要的人有借鉴之处。
注:经过压缩DataSet的WebService就缺少了通用性,请权衡使用之。
测试源码(仅压缩部分)和结果如下:
条件:压缩前的DataSet的大小为16891323字节(大约16.12M)。
环境:.Net Formatwork 1.1
系统: Windows XP SP1
硬件:P4(2.4G) + 256DDR
方案1:DataSetSurrogate + BinaryFormatter
Public Function DataSetToByte(ByVal DataSet As DataSet) As Byte()
Dim DSS As New DataSetSurrogate(DataSet)
Dim MS As New IO.MemoryStream
Dim BF As New BinaryFormatter
BF.Serialize(MS, DSS)
Dim ReVal() As Byte = MS.ToArray()
MS.Close()
DSS = Nothing
MS = Nothing
BF = Nothing
Return ReVal
End Function
方案2:DataSetSurrogate + BinaryFormatter + SharpZipLib
Public Function DataSetToByte(ByVal DataSet As DataSet) As Byte()
Dim MS As New IO.MemoryStream
Dim ZOS As New ZipOutputStream(MS)
ZOS.PutNextEntry(New ZipEntry(DataSet.DataSetName))
Dim BF As New BinaryFormatter
Dim DSS As DataSetSurrogate = New DataSetSurrogate(DataSet)
BF.Serialize(ZOS, DSS)
ZOS.CloseEntry()
ZOS.Close()
Dim ReVal As Byte() = MS.ToArray
MS.Close()
MS = Nothing
ZOS = Nothing
BF = Nothing
DSS = Nothing
Return ReVal
End Function
方案3:zlib
Public Function DataSetToByte(ByVal DataSet As DataSet) As Byte()
Dim MS As New System.IO.MemoryStream
DataSet.WriteXml(MS, XmlWriteMode.WriteSchema)
Dim ReVal() As Byte = MS.ToArray
MS.Close()
MS = Nothing
Dim zlib As New vbZLib.Compress
zlib.CompressByte(ReVal)
zlib = Nothing
Return ReVal
End Function
结果(压缩后):
方案1:4420881字节(被压缩掉大约73.83%),耗时:23200ms
方案2: 696881字节(被压缩掉大约95.87%),耗时:26621ms
方案3: 422990字节(被压缩掉大约97.50%),耗时: 680ms
看来,无论压缩率还是耗时都是方案3最优,但是使用了第三方组件。前两个方案对CPU资源占用率的也让人无法接受。
本文地址:http://www.45fan.com/dnjc/67499.html