Skip to content

Instantly share code, notes, and snippets.

@mgravell
Last active November 10, 2017 19:58
Show Gist options
  • Save mgravell/2cb66770cde42f7e369c90c39290ea15 to your computer and use it in GitHub Desktop.
Save mgravell/2cb66770cde42f7e369c90c39290ea15 to your computer and use it in GitHub Desktop.
xml serializer fun
default serializer
i=0; 32ms, 18 assemblies
i=500; 40ms, 18 assemblies
i=1000; 46ms, 18 assemblies
i=1500; 52ms, 18 assemblies
i=2000; 57ms, 18 assemblies
i=2500; 63ms, 18 assemblies
i=3000; 68ms, 18 assemblies
i=3500; 74ms, 18 assemblies
i=4000; 79ms, 18 assemblies
i=4500; 84ms, 18 assemblies
all done; 90ms, 18 assemblies
custom serializer (with cache)
i=0; 6ms, 20 assemblies
i=500; 8ms, 20 assemblies
i=1000; 10ms, 20 assemblies
i=1500; 11ms, 20 assemblies
i=2000; 13ms, 20 assemblies
i=2500; 14ms, 20 assemblies
i=3000; 16ms, 20 assemblies
i=3500; 18ms, 20 assemblies
i=4000; 19ms, 20 assemblies
i=4500; 21ms, 20 assemblies
all done; 22ms, 20 assemblies
custom serializer (no cache)
i=0; 4ms, 21 assemblies
i=500; 1950ms, 521 assemblies
i=1000; 3903ms, 1021 assemblies
i=1500; 5661ms, 1521 assemblies
i=2000; 7639ms, 2021 assemblies
i=2500; 9507ms, 2521 assemblies
i=3000; 11459ms, 3021 assemblies
i=3500; 13366ms, 3521 assemblies
i=4000; 15396ms, 4021 assemblies
i=4500; 17368ms, 4521 assemblies
all done; 19372ms, 5020 assemblies
using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Xml.Serialization;
static class Program
{
static void Main()
{
RunTest("default serializer", DefaultSerializer);
RunTest("custom serializer (with cache)", CustomSerializerCache);
RunTest("custom serializer (no cache)", CustomSerializerNoCache);
}
static void Collect()
{
for(int i = 0; i < 3; i++)
{
GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced);
GC.WaitForPendingFinalizers();
}
}
static void RunTest(string caption, Action<Stream, Foo> action)
{
var ms = new MemoryStream();
var foo = new Foo { Id = 1 };
Console.WriteLine(caption);
var timer = Stopwatch.StartNew();
for(int i = 0; i < 5000; i++)
{
ms.SetLength(0);
action(ms, foo);
if( (i % 500) == 0)
{
Console.WriteLine($"i={i}; {timer.ElapsedMilliseconds}ms, {AppDomain.CurrentDomain.GetAssemblies().Length} assemblies");
}
}
timer.Stop();
Collect();
Console.WriteLine($"all done; {timer.ElapsedMilliseconds}ms, {AppDomain.CurrentDomain.GetAssemblies().Length} assemblies");
Console.WriteLine();
}
static void DefaultSerializer(Stream s, Foo foo)
{
var ser = new XmlSerializer(typeof(Foo));
ser.Serialize(s, foo);
}
static readonly XmlAttributeOverrides _customOverrides = GetCustomOverrides();
static XmlAttributeOverrides GetCustomOverrides()
{
var ao = new XmlAttributeOverrides();
ao.Add(typeof(Foo), new XmlAttributes
{
XmlRoot = new XmlRootAttribute("bar")
});
return ao;
}
static XmlSerializer _ser;
static void CustomSerializerCache(Stream s, Foo foo)
{
var ser = _ser ?? (_ser = new XmlSerializer(typeof(Foo), _customOverrides));
ser.Serialize(s, foo);
}
static void CustomSerializerNoCache(Stream s, Foo foo)
{
var ser = new XmlSerializer(typeof(Foo), _customOverrides);
ser.Serialize(s, foo);
}
}
public class Foo
{
public int Id { get; set; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment