这个问题在不同的网站上提出但我找不到任何有用的答案,但仍然存在一些性能问题。 我的公共层应用程序中有两个序列化方法
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服务器并获取响应的是否有提高效率的依据?
答案 0 :(得分:0)
我写了以下代码,但没有发现任何重大差异。但是,通过泛型序列化会稍快一些。这是代码:
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();
}
}
也许最好与我们分享一个类,您正在尝试序列化/反序列化并共享代码,通过这些代码来估算序列化方法的执行时间。