|
-
May 25th, 2010, 11:18 PM
#1
WCF serialization optimization
In recent research, i found that if we serialize the same type of object twice, the second one takes much less time. Does anyone know the reason for this?
Take following codes as an example, the first time serialization would take 30 ms , while second time it reduced to less than 1 ms. The difference becomes much bigger when the object which is being serialized has more fields.
namespace ConsoleApplication3
{
[DataContract]
class MyData
{
[DataMember]
public string Name;
[DataMember]
public int Age;
}
class Program
{
static void Main(string[] args)
{
MyData data = new MyData() { Name = "xuning", Age = 12 };
var dcs = new DataContractSerializer(typeof(MyData));
Stopwatch watch = new Stopwatch();
using (XmlWriter fs = new XmlTextWriter("PamPos.xml", Encoding.UTF8))
{
watch.Reset();
watch.Start();
dcs.WriteObject(fs, data);
watch.Stop();
Console.WriteLine(watch.ElapsedMilliseconds);
}
var dcs2 = new DataContractSerializer(typeof(MyData));
using (XmlWriter fs = new XmlTextWriter("PamPos2.xml", Encoding.UTF8))
{
watch.Reset();
watch.Start();
dcs2.WriteObject(fs, data);
fs.Close();
watch.Stop();
Console.WriteLine(watch.ElapsedMilliseconds);
}
}
}
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|