net自带二进制序列化,XML序列化和ProtoBuf序列化的压缩对比
測(cè)試結(jié)果:
ProtoBuf Length:115
BinaryFormatter Length:1177
XmlSerializer Length:814
xml length:825
做了一個(gè)各種序列化方案的壓縮比例測(cè)試,可以看到protobuf序列化后的大小是xml原始格式的8分之一,是xml序列化后的8分之一,是二進(jìn)制序列化的10分之一,總體看來(lái)ProtoBuf的優(yōu)勢(shì)還是很明顯的,不過(guò)ProtoBuf.net不是google官方提供的,也許和其它平臺(tái)不兼容,但如果做.NET服務(wù)端應(yīng)用,兩邊都是.NET,還是可以適用的,即使有一邊不是.NET,反正是開(kāi)源的東西,協(xié)議也有,也可以自己實(shí)現(xiàn)相應(yīng)語(yǔ)言的兼容ProtoBuf.net的協(xié)議棧。
SOAPFormatter沒(méi)有測(cè)試,一般用的不多。還有就是怪事了,為什么二進(jìn)制序列化反而大呢,奇怪了。
本次測(cè)試主要考慮協(xié)議的壓縮率的比較,不考慮序列化/解序列化的速度,官方聲明比XML解析要快幾十倍,有空看下它的實(shí)現(xiàn)代碼,我的SVN老下載不下來(lái)code.google的代碼,汗了。
測(cè)試代碼如下
internal?class?Program{
????private?static?void?Main(string[]?args)
????{
????????MemoryStream?ms?=?null;
????????Customer?customer?=?Customer.GetOneCustomer();
????????using?(ms?=?new?MemoryStream())
????????{
????????????Serializer.Serialize(ms,?customer);
????????????Console.WriteLine("ProtoBuf?Length:{0}",?ms.Length);
????????}
????????using?(ms?=?new?MemoryStream())
????????{
????????????var?formater?=?new?BinaryFormatter();
????????????formater.Serialize(ms,?customer);
????????????Console.WriteLine("BinaryFormatter?Length:{0}",?ms.Length);
????????}
????????using?(ms?=?new?MemoryStream())
????????{
????????????var?serializer?=?new?XmlSerializer(typeof?(Customer));
????????????serializer.Serialize(ms,?customer);
????????????Console.WriteLine("XmlSerializer?Length:{0}",?ms.Length);
????????}
????????string?xml?=
????????????@"<?xml?version=""1.0""??>
<Customer?xmlns=""urn:Sep2003Example"">
<CustomerID>ALFKI</CustomerID>
<PO>9572658</PO>
<Address>
????<Street>One?Main?Street</Street>
????<City>Anywhere</City>
????<State>NJ</State>
????<Zip>08080</Zip>
</Address>
<Order>
????<OrderID>10966</OrderID?>
????<LineItem>
????????<ProductID>37</ProductID>
????????<UnitPrice>26.50?</UnitPrice>
????????<Quantity>8</Quantity>
????????<Description>Gravad?lax?</Description>????????????
????</LineItem>
????<LineItem>
????????<ProductID>56?</ProductID>
????????<UnitPrice>38.00</UnitPrice>
????????<Quantity>12</Quantity>
????????<Description>Gnocchi?di?nonna?Alice</Description>????????????
????</LineItem>
</Order>????
</Customer>";
????????Console.WriteLine("xml?length:{0}",?Encoding.UTF8.GetByteCount(xml));
????????Console.ReadKey();
????}
}
相關(guān)數(shù)據(jù)結(jié)構(gòu)如下?
?
[ProtoContract][Serializable]
public?class?Customer?{
????[ProtoMember(1)]
????public?string?CustomerID?{?get;?set;?}
????[ProtoMember(2)]
????public?int?PO?{?get;?set;?}
????[ProtoMember(3)]
????public?Address?Address?{?get;?set;?}
????[ProtoMember(4)]
????public?Order?Order?{?get;?set;?}
????public?static?Customer?GetOneCustomer()?{
????????Customer?customer?=?new?Customer?{
????????????CustomerID?=?"ALFKI",
????????????PO?=?9572658,
????????????Address?=?new?Address?{
????????????????Street?=?"One?Main?Street",
????????????????City?=?"Anywhere",
????????????????State?=?"NJ",
????????????????Zip?=?08080
????????????},
????????????Order?=?new?Order?{
????????????????OrderID?=?10966,
????????????????LineItems?=?new?List<LineItem>
????????????????????{
????????????????????????new?LineItem
????????????????????????????{
????????????????????????????????ProductID?=?37,
????????????????????????????????UnitPrice?=?26.50M,
????????????????????????????????Quantity?=8,
????????????????????????????????Description?="Gravad?lax"
????????????????????????????},
????????????????????????new?LineItem
????????????????????????????{
????????????????????????????????ProductID?=?56,
????????????????????????????????UnitPrice?=?38.00M,
????????????????????????????????Quantity?=12,
????????????????????????????????Description?="Gnocchi?di?nonna?Alice"????
????????????????????????????}
????????????????????}
????????????}
????????};
????????return?customer;
????}
}
[ProtoContract]
[Serializable]
public?class?Address?{
????[ProtoMember(1)]
????public?string?Street?{?get;?set;?}
????[ProtoMember(2)]
????public?string?City?{?get;?set;?}
????[ProtoMember(3)]
????public?string?State?{?get;?set;?}
????[ProtoMember(4)]
????public?int?Zip?{?get;?set;?}
}
[ProtoContract]
[Serializable]
public?class?Order?{
????[ProtoMember(1)]
????public?int?OrderID?{?get;?set;?}
????[ProtoMember(2)]
????public?List<LineItem>?LineItems?{?get;?set;?}
}
[ProtoContract]
[Serializable]
public?class?LineItem?{
????[ProtoMember(1)]
????public?int?ProductID?{?get;?set;?}
????[ProtoMember(2)]
????public?decimal?UnitPrice?{?get;?set;?}
????[ProtoMember(3)]
????public?int?Quantity?{?get;?set;?}
????[ProtoMember(4)]
????public?string?Description?{?get;?set;?}
}
相關(guān)鏈接
Protocol Buffers 性能測(cè)試
http://hellobmw.com/archives/protocol-buffers-performance.html
Windows Communication Protocols (MCPP)
http://msdn.microsoft.com/en-us/library/cc216513(PROT.10).aspx
淺談如何使用.NET存儲(chǔ)XML數(shù)據(jù)
http://developer.51cto.com/art/200905/122238.htm
.net下二進(jìn)制序列化的格式分析[轉(zhuǎn)]
http://www.cnblogs.com/lxinxuan/archive/2006/09/06/496340.html
Protocol Buffers Encoding
http://code.google.com/intl/zh-CN/apis/protocolbuffers/docs/encoding.html
總結(jié)
以上是生活随笔為你收集整理的net自带二进制序列化,XML序列化和ProtoBuf序列化的压缩对比的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 停止坐井观天,是时候让“我个人认为”见鬼
- 下一篇: ogre研究之第一个程序(一)