Skip to content

Instantly share code, notes, and snippets.

@andreabalducci
Created September 6, 2012 09:25
Show Gist options
  • Save andreabalducci/3653680 to your computer and use it in GitHub Desktop.
Save andreabalducci/3653680 to your computer and use it in GitHub Desktop.
Fasterflect performance test
[Test, Explicit]
public void fasterflect_performance_test()
{
var handler = new SampleEventHandler();
var @event = new SampleEvent();
int iterations = 10000000;
// fasterflect
{
handler.Counter = 0;
var t = new Stopwatch();
var invoker = typeof(SampleEventHandler).DelegateForCallMethod("On", Flags.InstancePublic, new[] { @event.GetType() });
t.Start();
for (int c = 0; c < iterations; c++)
{
invoker.Invoke(handler, @event);
}
t.Stop();
Assert.AreEqual(iterations, handler.Counter);
Debug.WriteLine("Fasterflect : " + t.ElapsedMilliseconds);
}
// reflection
{
handler.Counter = 0;
var t = new Stopwatch();
var mi = typeof(SampleEventHandler).GetMethod("On");
var par = new object[] {@event};
t.Start();
for (int c = 0; c < iterations; c++)
{
mi.Invoke(handler, par);
}
t.Stop();
Assert.AreEqual(iterations, handler.Counter);
Debug.WriteLine("Reflection : " + t.ElapsedMilliseconds);
}
}
Fasterflect : 424
Reflection : 12302
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment