Serialization protobuf基元类型数组的网络序列化

Serialization protobuf基元类型数组的网络序列化,serialization,deserialization,protobuf-net,Serialization,Deserialization,Protobuf Net,我正在尝试使用protobuf net对一个简单的整数数组进行序列化/反序列化。然而,.NET二进制格式化程序的性能似乎比protobuf NET要好,这不是我所期望的。下面是我用来测试性能的代码: [ProtoContract] [Serializable] public struct Vector3Double { [ProtoMember(1)] public double x; [ProtoMember(2)] public double y; [ProtoMembe

我正在尝试使用protobuf net对一个简单的整数数组进行序列化/反序列化。然而,.NET二进制格式化程序的性能似乎比protobuf NET要好,这不是我所期望的。下面是我用来测试性能的代码:

[ProtoContract]
[Serializable]
public struct Vector3Double {
  [ProtoMember(1)]
  public double x;
  [ProtoMember(2)]
  public double y;
  [ProtoMember(3)]
  public double z;
}

public static void TestSerialization() {
  var v3ds = new Vector3Double[512 * 512];
  var ints = new int[512 * 512];
  for (var i = 0; i < v3ds.Length; i++) {
    v3ds[i].x = i;
    v3ds[i].y = i;
    v3ds[i].z = i;
  }
  var tmpFilePath = System.IO.Path.GetTempFileName();

  System.Console.WriteLine("BinaryFormatter");
  System.Console.WriteLine("---------------");
  using (var fs = System.IO.File.Open(tmpFilePath, System.IO.FileMode.Create)) {
    System.Console.Write("Array of Vector3Double (serialization): ");
    var bf = new BinaryFormatter();
    var sw = new Stopwatch();
    sw.Start();
    bf.Serialize(fs, v3ds);
    sw.Stop();
    Console.WriteLine(sw.ElapsedMilliseconds);
  }

  using (var fs = System.IO.File.Open(tmpFilePath, System.IO.FileMode.Open)) {
    System.Console.Write("Array of Vector3Double (deserialization): ");
    var bf = new BinaryFormatter();
    var sw = new Stopwatch();
    sw.Start();
    var des = bf.Deserialize(fs);
    sw.Stop();
    Console.WriteLine(sw.ElapsedMilliseconds);
  }

  System.Console.WriteLine();
  System.Console.WriteLine("ProtoBuf Serializer");
  System.Console.WriteLine("-------------------");
  using (var fs = System.IO.File.Open(tmpFilePath, System.IO.FileMode.Create)) {
    System.Console.Write("Array of Vector3Double (serialization): ");
    var sw = new Stopwatch();
    sw.Start();
    ProtoBuf.Serializer.Serialize(fs, v3ds);
    sw.Stop();
    Console.WriteLine(sw.ElapsedMilliseconds);
  }

  using (var fs = System.IO.File.Open(tmpFilePath, System.IO.FileMode.Open)) {
    System.Console.Write("Array of Vector3Double (deserialization): ");
    var sw = new Stopwatch();
    sw.Start();
    var des = ProtoBuf.Serializer.Deserialize<Vector3Double[]>(fs);
    sw.Stop();
    Console.WriteLine(sw.ElapsedMilliseconds);
  }

  System.Console.WriteLine("BinaryFormatter");
  System.Console.WriteLine("---------------");
  using (var fs = System.IO.File.Open(tmpFilePath, System.IO.FileMode.Create)) {
    System.Console.Write("Array of int (serialization): ");
    var bf = new BinaryFormatter();
    var sw = new Stopwatch();
    sw.Start();
    bf.Serialize(fs, ints);
    sw.Stop();
    Console.WriteLine(sw.ElapsedMilliseconds);
  }

  using (var fs = System.IO.File.Open(tmpFilePath, System.IO.FileMode.Open)) {
    System.Console.Write("Array of int (deserialization): ");
    var bf = new BinaryFormatter();
    var sw = new Stopwatch();
    sw.Start();
    var des = bf.Deserialize(fs);
    sw.Stop();
    Console.WriteLine(sw.ElapsedMilliseconds);
  }

  System.Console.WriteLine();
  System.Console.WriteLine("ProtoBuf Serializer");
  System.Console.WriteLine("-------------------");
  using (var fs = System.IO.File.Open(tmpFilePath, System.IO.FileMode.Create)) {
    System.Console.Write("Array of int (serialization): ");
    var sw = new Stopwatch();
    sw.Start();
    ProtoBuf.Serializer.Serialize(fs, ints);
    sw.Stop();
    Console.WriteLine(sw.ElapsedMilliseconds);
  }

  using (var fs = System.IO.File.Open(tmpFilePath, System.IO.FileMode.Open)) {
    System.Console.Write("Array of int (deserialization): ");
    var sw = new Stopwatch();
    sw.Start();
    var des = ProtoBuf.Serializer.Deserialize<int[]>(fs);
    sw.Stop();
    Console.WriteLine(sw.ElapsedMilliseconds);
  }
}
因此,BinaryFormatter在大约1到2毫秒内序列化/反序列化一个整数数组,而protobuf net序列化程序/反序列化程序需要57到60毫秒。我在上面的代码中是否有错误?或者protobuf网络不适合于基本数组

更新日期2016/04/29

我对protobuf-net-r668源代码进行了一点调试,似乎数组一次只写入一项到输出流中(在TypeModel.cs中的TrySerializeAuxiliaryType中)。是否可以指定数组应一次性写入输出流?或者我应该修改源代码来序列化/反序列化我的使用场景中的数组吗

BinaryFormatter
---------------
Array of Vector3Double (serialization): 401
Array of Vector3Double (deserialization): 1266

ProtoBuf Serializer
-------------------
Array of Vector3Double (serialization): 206
Array of Vector3Double (deserialization): 186

BinaryFormatter
---------------
Array of int (serialization): 2
Array of int (deserialization): 1

ProtoBuf Serializer
-------------------
Array of int (serialization): 57
Array of int (deserialization): 60