日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

net自带二进制序列化,XML序列化和ProtoBuf序列化的压缩对比

發(fā)布時(shí)間:2025/3/8 asp.net 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 net自带二进制序列化,XML序列化和ProtoBuf序列化的压缩对比 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

測(cè)試結(jié)果:
ProtoBuf Length:115
BinaryFormatter Length:1177
XmlSerializer Length:814
xml length:825
做了一個(gè)各種序列化方案的壓縮比例測(cè)試,可以看到protobuf序列化后的大小是xml原始格式的8分之一,是xml序列化后的8分之一,是二進(jìn)制序列化的10分之一,總體看來ProtoBuf的優(yōu)勢(shì)還是很明顯的,不過ProtoBuf.net不是google官方提供的,也許和其它平臺(tái)不兼容,但如果做.NET服務(wù)端應(yīng)用,兩邊都是.NET,還是可以適用的,即使有一邊不是.NET,反正是開源的東西,協(xié)議也有,也可以自己實(shí)現(xiàn)相應(yīng)語言的兼容ProtoBuf.net的協(xié)議棧。
SOAPFormatter沒有測(cè)試,一般用的不多。還有就是怪事了,為什么二進(jìn)制序列化反而大呢,奇怪了。
本次測(cè)試主要考慮協(xié)議的壓縮率的比較,不考慮序列化/解序列化的速度,官方聲明比XML解析要快幾十倍,有空看下它的實(shí)現(xiàn)代碼,我的SVN老下載不下來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ǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。