本文介绍了XML二进制VS性能,序列化/反序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个小巧的框架应用程序和需要来提高性能。该应用程序目前通过序列化对象到XML并将其存储在数据库中的脱机工作。使用分析工具,我可以看到,这是一个相当大的开销,减缓了应用程序。我想,如果我切换到一个二进制序列化的表现会增加,但由于这是不支持Compact Framework的我看着protobuf网。序列化似乎更快,但反序列化慢得多,应用程序正在做更多的比反序列化序列化。

如果二进制序列化应该会更快,如果是这样我能做些什么,以加快的表现?下面是我如何使用XML和二进制的一个片段:

XML序列化:

 公共字符串序列化(T obj)以
{
  UTF8Encoding编码=新UTF8Encoding();
  XmlSerializer的序列化=新的XmlSerializer(typeof运算(T));
  MemoryStream的流=新的MemoryStream();
  XmlTextWriter的作家=新的XmlTextWriter(流Encoding.UTF8);
  serializer.Serialize(流OBJ);
  流=(MemoryStream的)writer.BaseStream;
  返回encoding.GetString(stream.ToArray(),0,Convert.ToInt32(stream.Length));
}
公共牛逼反序列化(XML字符串)
{
  UTF8Encoding编码=新UTF8Encoding();
  XmlSerializer的序列化=新的XmlSerializer(typeof运算(T));
  MemoryStream的流=新的MemoryStream(encoding.GetBytes(XML));
  返程(T)serializer.Deserialize(流);
}
 

protobuf网二进制序列:

 公共byte []的序列化(T obj)以
{
  byte []的原料;
  使用(MemoryStream的MemoryStream的=新的MemoryStream())
  {
    Serializer.Serialize(MemoryStream的,OBJ);
    生= memoryStream.ToArray();
  }

  回归原始;
}

公共牛逼反序列化(byte []的serializedType)
{
  牛逼OBJ;
  使用(MemoryStream的MemoryStream的=新的MemoryStream(serializedType))
  {
    OBJ = Serializer.Deserialize< T>(MemoryStream的);
  }
  返回OBJ;
}
 

解决方案

我要纠正自己对这个,马克Gravall指出,在第一次迭代已经BULDING模型的开销,所以我已经做了一些测试,取平均1000次迭代序列化和反序列化的XML和二进制。我想我的测试与Compact Framework的DLL的V2,然后再与v3.5版本的DLL。这是我得到的,时间是在毫秒:

  .NET 2.0
================================ XML ======二进制===
连载第1次迭代3236 5508
反序列化第一次迭代1501 318
序列化平均9.826 5.525
反序列化平均5.525 0.771

.NET 3.5
================================ XML ======二进制===
连载第1次迭代3307 5598
反序列化第一次迭代1386 200
序列化平均10.923 5.605
反序列化平均5.605 0.279
 

I'm working on a compact framework application and need to boost performance. The app currently works offline by serializing objects to XML and storing them in a database. Using a profiling tool I could see this was quite a big overhead, slowing the app. I thought if I switched to a binary serialization the performance would increase, but because this is not supported in the compact framework I looked at protobuf-net. The serialization seems quicker, but deserialization much slower and the app is doing more deserializing than serializing.

Should binary serialization should be faster and if so what I can do to speed up the performance? Here's a snippet of how I'm using both XML and binary:

XML serialization:

public string Serialize(T obj)
{
  UTF8Encoding encoding = new UTF8Encoding();
  XmlSerializer serializer = new XmlSerializer(typeof(T));
  MemoryStream stream = new MemoryStream();
  XmlTextWriter writer = new XmlTextWriter(stream, Encoding.UTF8);
  serializer.Serialize(stream, obj);
  stream = (MemoryStream)writer.BaseStream;
  return encoding.GetString(stream.ToArray(), 0, Convert.ToInt32(stream.Length));
}
public T Deserialize(string xml)
{
  UTF8Encoding encoding = new UTF8Encoding();
  XmlSerializer serializer = new XmlSerializer(typeof(T));
  MemoryStream stream = new MemoryStream(encoding.GetBytes(xml));
  return (T)serializer.Deserialize(stream);
}

Protobuf-net Binary serialization:

public byte[] Serialize(T obj)
{
  byte[] raw;
  using (MemoryStream memoryStream = new MemoryStream())
  {
    Serializer.Serialize(memoryStream, obj);
    raw = memoryStream.ToArray();
  }

  return raw;
}

public T Deserialize(byte[] serializedType)
{
  T obj;
  using (MemoryStream memoryStream = new MemoryStream(serializedType))
  {
    obj = Serializer.Deserialize<T>(memoryStream);
  }
  return obj;
}
解决方案

I'm going to correct myself on this, Marc Gravall pointed out the first iteration has an overhead of bulding the model so I've done some tests taking the average of 1000 iterations of serialization and deserialization for both XML and binary. I tried my tests with the v2 of the Compact Framework DLL first, and then with the v3.5 DLL. Here's what I got, time is in ms:

.NET 2.0
================================ XML ====== Binary ===
Serialization 1st Iteration      3236       5508
Deserialization 1st Iteration    1501       318
Serialization Average            9.826      5.525
Deserialization Average          5.525      0.771

.NET 3.5
================================ XML ====== Binary ===
Serialization 1st Iteration      3307       5598
Deserialization 1st Iteration    1386       200
Serialization Average            10.923     5.605
Deserialization Average          5.605      0.279

这篇关于XML二进制VS性能,序列化/反序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 08:36