Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c#二进制格式化程序速度慢_C#_Serialization_Binaryformatter - Fatal编程技术网

c#二进制格式化程序速度慢

c#二进制格式化程序速度慢,c#,serialization,binaryformatter,C#,Serialization,Binaryformatter,我使用BinaryFormatter将对象序列化/反序列化为字节数组。但是太慢了。这是我的密码: IFormatter formatter = new BinaryFormatter(); MemoryStream stream = new MemoryStream(); formatter.Serialize(stream, this); stream.Close(); byte[] currentByteArray = stream.ToArray(); 是否有可能改进该代码,以加快速度。

我使用BinaryFormatter将对象序列化/反序列化为字节数组。但是太慢了。这是我的密码:

IFormatter formatter = new BinaryFormatter();
MemoryStream stream = new MemoryStream();
formatter.Serialize(stream, this);
stream.Close();
byte[] currentByteArray = stream.ToArray();
是否有可能改进该代码,以加快速度。或者我的选择是什么?我见过其他几种序列化方法,比如xmlserialization,但我不想将其作为字节数组写入文件


提前谢谢

如果您在
finally
语句中加入如下内容,您的代码可以得到改进:

IFormatter formatter;
MemoryStream stream;
try
{
    formatter = new BinaryFormatter();
    stream = new MemoryStream();
    formatter.Serialize(stream, this);
    byte[] currentByteArray = stream.ToArray();
}
finally
{
   if(stream!=null)
      stream.Close();
}
但是,上面的代码并不能提高BinaryFormatter类的性能,因为它可以正常工作并正确使用。但是您可以使用其他库

NET中速度最快且通用的序列化程序之一是。例如:

[ProtoContract]
class SubMessageRepresentations
{
   [ProtoMember(5, DataFormat = DataFormat.Default)] 
   public SubObject lengthPrefixedObject;
   [ProtoMember(6, DataFormat = DataFormat.Group)]
   public SubObject groupObject;
}

[ProtoContract(ImplicitFields=ImplicitFields.AllFields)]
class SubObject { public int x; }


using (var stream = new MemoryStream()) {
  _pbModel.Serialize(
   stream, new SubMessageRepresentations {
        lengthPrefixedObject = new SubObject { x = 0x22 },
        groupObject = new SubObject { x = 0x44 }
   });
byte[] buf = stream.GetBuffer();
for (int i = 0; i < stream.Length; i++)
Console.Write("{0:X2} ", buf[i]);
}
[协议]
课堂演示
{
[原始成员(5,DataFormat=DataFormat.Default)]
公共子对象长度前缀执行对象;
[原成员(6,DataFormat=DataFormat.Group)]
公共子对象groupObject;
}
[协议(ImplicitFields=ImplicitFields.AllFields)]
类子对象{public int x;}
使用(var stream=new MemoryStream()){
_pbModel.Serialize(
流,新的子消息{
lengthPrefixedObject=新子对象{x=0x22},
groupObject=新子对象{x=0x44}
});
字节[]buf=stream.GetBuffer();
for(int i=0;i
如果您在
finally
语句中加入处置,您的代码可以得到改进,就像大家在评论中所说的:

IFormatter formatter;
MemoryStream stream;
try
{
    formatter = new BinaryFormatter();
    stream = new MemoryStream();
    formatter.Serialize(stream, this);
    byte[] currentByteArray = stream.ToArray();
}
finally
{
   if(stream!=null)
      stream.Close();
}
但是,上面的代码并不能提高BinaryFormatter类的性能,因为它可以正常工作并正确使用。但是您可以使用其他库

NET中速度最快且通用的序列化程序之一是。例如:

[ProtoContract]
class SubMessageRepresentations
{
   [ProtoMember(5, DataFormat = DataFormat.Default)] 
   public SubObject lengthPrefixedObject;
   [ProtoMember(6, DataFormat = DataFormat.Group)]
   public SubObject groupObject;
}

[ProtoContract(ImplicitFields=ImplicitFields.AllFields)]
class SubObject { public int x; }


using (var stream = new MemoryStream()) {
  _pbModel.Serialize(
   stream, new SubMessageRepresentations {
        lengthPrefixedObject = new SubObject { x = 0x22 },
        groupObject = new SubObject { x = 0x44 }
   });
byte[] buf = stream.GetBuffer();
for (int i = 0; i < stream.Length; i++)
Console.Write("{0:X2} ", buf[i]);
}
[协议]
课堂演示
{
[原始成员(5,DataFormat=DataFormat.Default)]
公共子对象长度前缀执行对象;
[原成员(6,DataFormat=DataFormat.Group)]
公共子对象groupObject;
}
[协议(ImplicitFields=ImplicitFields.AllFields)]
类子对象{public int x;}
使用(var stream=new MemoryStream()){
_pbModel.Serialize(
流,新的子消息{
lengthPrefixedObject=新子对象{x=0x22},
groupObject=新子对象{x=0x44}
});
字节[]buf=stream.GetBuffer();
for(int i=0;i
请在您的问题中包含类代码和度量。此外,序列化数据有多大?我们在这里讨论的是什么大小?
stream.Close()应该在
字节[]currentByteArray=stream.ToArray()之后,而不是在序列化数据对象之前?是否用于通信?@DmitryBychenko否。它应该位于
ToArray()之后的
finally
块中。事实上,流应该只在使用
块的
中。请在您的问题中包括类代码和度量。此外,序列化数据有多大?我们在这里讨论的是什么大小?
stream.Close()应该在
字节[]currentByteArray=stream.ToArray()之后,而不是在序列化数据对象之前?是否用于通信?@DmitryBychenko否。它应该位于
ToArray()之后的
finally
块中。事实上,流应该正好位于使用
块的
中。