Skip to content

Instantly share code, notes, and snippets.

@davidfowl
Created July 24, 2012 22:06
Show Gist options
  • Save davidfowl/3172990 to your computer and use it in GitHub Desktop.
Save davidfowl/3172990 to your computer and use it in GitHub Desktop.
MethodInfo.Invoke is slow...
using System;
using System.Diagnostics;
using System.Linq.Expressions;
using System.Reflection;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
var mi = typeof(A).GetMethod("Foo");
Console.WriteLine("Regular method");
Time(() => new A().Foo(1, 2));
Console.WriteLine();
Console.WriteLine("Invoke");
Time(() => mi.Invoke(new A(), new object[] { 1, 2 }));
Console.WriteLine();
Console.WriteLine("Cached Delegate");
var cache = Compile(mi);
Time(() => cache.Invoke(new A(), 1, 2));
}
private static void Time(Action a)
{
var sw = Stopwatch.StartNew();
for (int i = 0; i < 10000000; i++)
{
a();
}
sw.Stop();
Console.WriteLine(sw.Elapsed);
}
private static Action<A, int, int> Compile(MethodInfo mi)
{
// Params
var p1 = Expression.Parameter(typeof(A));
var p2 = Expression.Parameter(typeof(int));
var p3 = Expression.Parameter(typeof(int));
// Body
var body = Expression.Call(p1, mi, p2, p3);
// compile
return Expression.Lambda<Action<A, int, int>>(body, p1, p2, p3).Compile();
}
}
public class A
{
public void Foo(int a, int b)
{
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment