简体   繁体   English

c#中的二进制序列化:结果大小和性能

[英]Binary serialization in c#: result size and performance

I'm trying to understand and to decide the best approach to my problem. 我正在努力理解并决定解决问题的最佳方法。 I've an xsd that represents the schema of the information that I agreed with a client. 我有一个xsd代表我与客户达成一致的信息架构。

Now, in my application (c#, .net3.5) I use and consume an object that has been deserialized from an xml created according to the xsd schema. 现在,在我的应用程序(c#,。net3.5)中,我使用并使用了一个已根据xsd模式创建的xml反序列化的对象。

As soon as I fill the object with data, I want to pass it to another application and also store it in a db. 只要我用数据填充对象,我想将它传递给另一个应用程序并将其存储在数据库中。 I have two questions: 我有两个问题:

  1. I'd like to serialize the object to pass quickly to the other application: is better binary or xml serialization? 我想序列化对象以快速传递给另一个应用程序:更好的二进制或xml序列化?
  2. Unfortunately in the db I have a limited sized field to store the info, so I need a sort of compression of the serialized object. 不幸的是在db中我有一个有限大小的字段来存储信息,所以我需要对序列化对象进行一种压缩。 Binary serialization creates smaller data then xml serialization or I need in any case to compress this data? 二进制序列化创建较小的数据然后xml序列化或我在任何情况下需要压缩这些数据? if yes, how? 如果有,怎么样?

Thanks! 谢谢!

I'd like to serialize the object to pass quickly to the other application: is better binary or xml serialization? 我想序列化对象以快速传递给另一个应用程序:更好的二进制或xml序列化?

Neither is specific enough; 两者都不够具体; binary can be good or bad; 二元可以是好的还是坏的; xml can be good or bad. xml可以是好的也可以是坏的。 Generally speaking binary is smaller and faster to process, but changing to such will be unusable from code that expects xml. 一般来说,二进制文件更小,处理速度更快,但是对于需要xml的代码,更改为二进制文件将无法使用。

Binary serialization creates smaller data then xml serialization or I need in any case to compress this data? 二进制序列化创建较小的数据然后xml序列化或我在任何情况下需要压缩这些数据?

It can be smaller; 可以更小; or it can be larger; 或者它可以更大; indeed, compression can make things smaller or larger too. 实际上,压缩也可以使事情变得更小或更大。

If space is your primary concern, I would suggest running it through something like protobuf-net (a binary serializer without the versioning issues common to BinaryFormatter ), and then speculatively try compressing it with GZipStream . 如果空间是你主要考虑的问题,我建议通过protobuf-net(二进制序列化器没有BinaryFormatter常见的版本控制问题)来运行它,然后推测性地尝试用GZipStream压缩它。 If the compressed version is smaller: store that (and a marker - perhaps a preamble - that says "I'm compressed"). 如果压缩版本较小:存储(和标记 - 可能是序言 - 说“我被压缩”)。 If the compressed version gets bigger than the original version, store the original (again with a preamble). 如果压缩版本比原始版本大,则存储原始版本(再次使用前导码)。

Here's a recent breakdown of the performance (speed and size) of the common .NET serializers: http://theburningmonk.com/2013/09/binary-and-json-serializer-benchmarks-updated/ 以下是常见.NET序列化程序的性能(速度和大小)的最新细分: http//theburningmonk.com/2013/09/binary-and-json-serializer-benchmarks-updated/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM