Serialization using Protobuf-net and Protobuf C#Port vs XML
I'm interested in a certain situation. I have an object in C# that I would like to serialize and deserialize.
I'm kind of conducting an experiment. I'm trying to see if switching the libraries of protobuf will have any affect on the time it takes to serialize and deserialize an object. Additionally, I'm throwing an XML serialization in the mix to see if that can compete as well, even though I'm pretty sure protobuf is faster.
In terms of speed, is there a clear, definite winner between protobuf and XML? Assuming everything was done consistently? i.e. Same path, parallel code, straightforward, etc.. And als开发者_Python百科o, would the speed be affected if I switched the libraries that the protobuf is using to serialize and deserialize? (From protobuf-net to protobuf C# port?) I'm quite new to this so I do not know the answer yet, but what I heard was that protobuf is supposed to be smaller, faster, and easier than XML.
Any insight is greatly appreciated! Thanks! Off to write the tests now.
Will protobuf be quicker? Absolutely. I've profiled this many many times, all with similar results, for examples:
- Performance Tests of Serializations used by WCF Bindings
- https://stackoverflow.com/questions/1650419/protocol-buffers-net-protobuf-net-10x-slower-that-xml-serializer-how-come/1660990#1660990 (note the title here is misleading)
- http://code.google.com/p/protobuf-net/wiki/Performance
- http://www.servicestack.net/benchmarks/NorthwindDatabaseRowsSerialization.1000000-times.2010-02-06.html (doesn't include
XmlSerializer
, but includes most others for comparison)
and many passing comments from happy users. I honenstly haven't compared to Jon's version in a long while, and I haven't done a direct v2 comparison, but here's a key point: in most cases the final bandwidth is the limiting factor in network performance, and they are the same wire format so should be pretty much identical there. Of course, protobuf is also demonstrably cheaper to read and write too, but unless you are on a mobile device that is secondary.
The big difference between protobuf-net and the port is that the ported version (Jon's) adopts (quite reasonably) the protobuf approach (immutable/generated objects etc) which might it hard to retrofit to an existing type model - you would have to introduce a separate DTO layer and map to it. Which isn't a big problem - simply a consideration. And for that reason you might find it hard to do a direct comparison between XmlSerializer and the port; they both get your data, but the routes are very different. Conversely, protobuf-net deliberately positions itself as a very similar API to XmlSerializer etc, so it is pretty easy to do a test suite using the same objects etc - just changing the serializer.
精彩评论