ProtoBuf-Net 性能 [英] ProtoBuf-Net Performance

查看:44
本文介绍了ProtoBuf-Net 性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的例子中:

Class1 序列化所需的时间几乎是 Class2 序列化的两倍吗?或者 protobuf-net 会将 Class1 中的 byte[] 作为已经序列化的数据处理?

Would Class1 serialization take almost twice as long as Class2 serialization? Or would protobuf-net handle the byte[] in Class1 as already serialized data?

伪示例:

[ProtoContract]
class Class1
{
  [ProtoMember(1)]
  public byte[] SerializedData { get; set; }
}

[ProtoContract]
class Class2
{
  [ProtoMember(1)]
  public Class3 NonSerializedData { get; set; }
}

[ProtoContract]
class Class3
{
  [ProtoMember(1)]
  public string Address{ get; set; }

  [ProtoMember(2)]
  public string ZipCode{ get; set; }

  [ProtoMember(2)]
  public string Country{ get; set; }
}

Class3 _c3 = new Class3() { Address = "MyAddress", ZipCode = "90210", Country = "AZ"  }

// Class 1 Serialization
Class1 _c1 = new C1();
_c1.SerializedData = protobuf.Serialize(_c3);

byte[] c1Bytes = protobuf.Serialize(_c1);

// Class 2 Serialization
Class2 _c2 = new Class2();
_c2.NonSerializedData = _c3;

byte[] c2Bytes = protobuf.Serialize(_c2);

推荐答案

或者 protobuf-net 会将 Class1 中的 byte[] 处理为已经序列化的数据?

Or would protobuf-net handle the byte[] in Class1 as already serialized data?

A byte[] 被视为原始数据(并在没有任何额外处理的情况下写入流),但仍有一些小事情需要序列化 ​​Class1 - 编写头字段和一般开销.在处理单个对象时,两种方法都足够快,任何差异都没有实际意义.但是,在大量情况下,我有充分的理由怀疑序列化 Class2 会明显更快 - 因为处理单个序列化管道可以避免许多开销

A byte[] is treated as raw data (and is written to the stream without any additional processing), but there are still some minor things needed to serialize Class1 - writing the header field, and general overheads. When dealing with individual objects, both approaches will be plenty quick enough that any difference is moot. In high volume, though, I have good reason to suspect that serializing Class2 would be noticeably faster - as dealing with a single serialization pipeline avoids a number of overheads

Class1 序列化所需的时间几乎是 Class2 序列化的两倍吗?

Would Class1 serialization take almost twice as long as Class2 serialization?

这将是一个很好的配置文件,但这将取决于您的典型数据.只有你能想出实际的数字.我希望稍微长一点".在这两个中,我正在比较序列化 class3 的成本,然后序列化由其输出组成的 class1"与序列化由 class1 实例组成的 class3 的成本"

That would be an excellent thing to profile, but it would depend on your typical data. Only you can come up with the actual number. I would expect "slightly longer". In both of these, I am comparing "cost of serializing class3, then serializing a class1 composed of the output from that" vs "cost of serializing a class3 composed of a class1 instance"

实际上,如果速度是您最关心的问题,我希望最佳方法是:

Actually, if speed is your primary concern, I would expect the optimum approach to be:

[ProtoContract]
class Class2
{
  [ProtoMember(1, DataFormat = DataFormat.Group)]
  public Class3 NonSerializedData { get; set; }
}

这个微妙的调整意味着它可以以非缓冲、只转发的方式编写 Class3(通过使用终止符而不是长度前缀).这不是默认值的唯一原因是谷歌明确更喜欢长度前缀方法.

This subtle tweak means that it can write Class3 in a non-buffered, forwards-only way (by using a terminator instead of a length-prefix). The only reason this isn't the default is that google explicitly prefer the length-prefix approach.

这篇关于ProtoBuf-Net 性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆