gpt4 book ai didi

c# - XML序列化与反序列化性能对比

转载 作者:太空宇宙 更新时间:2023-11-03 15:44:33 26 4
gpt4 key购买 nike

这个问题在不同的网站上提出过,但我找不到任何有用的答案,而且我仍然遇到一些性能问题。我的公共(public)层应用程序中有两个序列化程序方法

    public static string Serializer(object o)
{
var x = new XmlSerializer(o.GetType());
var writer = new StringWriter();
var xmlWriter = XmlWriter.Create(writer, new XmlWriterSettings { OmitXmlDeclaration = true });
var emptyNs = new XmlSerializerNamespaces(new[] { XmlQualifiedName.Empty });
x.Serialize(xmlWriter, o, emptyNs);
return writer.ToString();
}

public static string Serializer<T>(T o)
{
var x = new XmlSerializer(typeof(T));
var writer = new StringWriter();
var xmlWriter = XmlWriter.Create(writer, new XmlWriterSettings { OmitXmlDeclaration = true });
x.Serialize(xmlWriter, o, new XmlSerializerNamespaces( new[] { XmlQualifiedName.Empty } ));
return writer.ToString();
}

和两种反序列化方法

    public static T Deserializer<T>(string objectData)
{
var serializer = new XmlSerializer(typeof(T));
T result;
using (TextReader reader = new StringReader(objectData))
{
result =(T) serializer.Deserialize(reader);
}
return result;
}

public static object Deserializer(object o, string filename)
{
object retVal;
var ser = new XmlSerializer(o.GetType());
using (var reader = XmlReader.Create(filename))
{
retVal = ser.Deserialize(reader);
}
return retVal;
}

我在两种序列化器方法中运行了不同的负载测试,所有测试都显示 Serializer<T>(T o)Serializer(object o) 慢,在我看来必须是相反的,因为 typeof()更快,并且类型与对象不同。我想先了解一下您的意见?
其次,在另一个名为

的方法中使用的序列化器和反序列化器方法
 public static TResponse  SendRequest <TRequest,TResponse>(TRequest rq, Uri requestUri) 

它负责将请求发送到 Web 服务器并获得响应,是否可以提高效率?

最佳答案

我编写了以下代码,但没有发现任何显着差异。不过,通过泛型进行序列化会稍微快一些。这是代码:

public class TestData {
public string Name { get; set; }
public string FullName { get; set; }
public string Address { get; set; }
public int PostalCode { get; set; }

public TestData() {

}
public TestData(string name, string fullName, string address, int postalCode) {
Name = name;
FullName = fullName;
Address = address;
PostalCode = postalCode;
}
}

public static class Program
{

public static string Serializer(object o)
{
var x = new XmlSerializer(o.GetType());
var writer = new StringWriter();
var xmlWriter = XmlWriter.Create(writer, new XmlWriterSettings { OmitXmlDeclaration = true });
var emptyNs = new XmlSerializerNamespaces(new[] { XmlQualifiedName.Empty });
x.Serialize(xmlWriter, o, emptyNs);
return writer.ToString();
}

public static string Serializer<T>(T o)
{
var x = new XmlSerializer(typeof(T));
var writer = new StringWriter();
var xmlWriter = XmlWriter.Create(writer, new XmlWriterSettings { OmitXmlDeclaration = true });
x.Serialize(xmlWriter, o, new XmlSerializerNamespaces(new[] { XmlQualifiedName.Empty }));
return writer.ToString();
}

public static void Main(string[] args) {
Random rand = new Random();
const int numberOfCycles = 1000000;

Stopwatch watch = new Stopwatch();
watch.Start();
for (int i = 0; i < numberOfCycles; i++) {
object data = new TestData("", "", "", rand.Next());
Serializer(data);
}
watch.Stop();

Console.WriteLine(string.Format("Through object:{0}", watch.ElapsedMilliseconds));

watch.Restart();
for (int i = 0; i < numberOfCycles; i++) {
Serializer(new TestData("", "", "", rand.Next()));
}
watch.Stop();
Console.WriteLine(string.Format("Through generic:{0}", watch.ElapsedMilliseconds));

Console.ReadLine();
}
}

也许最好与我们分享您尝试序列化/反序列化的类,并分享您用来估算序列化方法执行时间的代码。

关于c# - XML序列化与反序列化性能对比,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28735593/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com