. Net built-in binary serialization, XML serialization and protobuf serialization compression comparison

Source: Internet
Author: User

Test results:
Protobuf length: 115
Binaryformatter length: 1177
Xmlserializer length: 814
XML length: 825
After testing the compression ratio of various serialization schemes, we can see that the size of protobuf after serialization is one of the eight points in the original XML format and one of the eight points after XML serialization, it is one of the 10 points of binary serialization. In general, protobuf has obvious advantages. However, protobuf.net is not officially provided by Google and may be incompatible with other platforms.. NET Server Applications, both of which are. net, even if one side is not. net, which is open-source, and has protocols. You can also implement protobuf.net protocol stacks compatible with the corresponding language.
Soapformatter is not tested and is rarely used. There is another strange thing. Why is binary serialization big? It's strange.
In this test, we mainly consider the Protocol compression rate comparison, not the serialization/deserialization speed. The official statement is dozens of times faster than XML parsing. If you have time, let's take a look at its implementation code, I cannot download code from SVN. google Code.

The test code is as follows:

Internal class Program
{
Private Static void main (string [] ARGs)
{
Memorystream MS = NULL;
Customer customer = Customer. getonecustomer ();

US (MS = new memorystream ())
{
Serializer. serialize (MS, customer );
Console. writeline ("protobuf length: {0}", ms. Length );
}
US (MS = new memorystream ())
{
VaR formater = new binaryformatter ();
Formater. serialized (MS, customer );
Console. writeline ("binaryformatter length: {0}", ms. Length );
}
US (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 ();
}
}


The related data structure is as follows:
 

 

[Protocontract]
[Serializable]
Public Class Customer {
[Protomember (1)]
Public String customerid {Get; set ;}
[Protomember (2)]
Public int Po {Get; set ;}
[Protomember (3)]
Public Address {Get; set ;}
[Protomember (4)]
Public 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.50 m,
Quantity = 8,
Description = "gravad lax"
},
New lineitem
{
Productid = 56,
Unitprice = 38.00 m,
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 ;}
}

Related Links
Protocol buffers performance test
Http://hellobmw.com/archives/protocol-buffers-performance.html
Windows communication protocols (MCPP)
Http://msdn.microsoft.com/en-us/library/cc216513 (prot.10). aspx
How to use. Net to store XML data
Http://developer.51cto.com/art/200905/122238.htm
Analysis of binary serialization formats in. Net [conversion]
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.