Created
November 19, 2009 22:38
-
-
Save kkozmic/239104 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Diagnostics; | |
namespace ConsoleApplication2 | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
//let the JITter kick in | |
TestNonGenericAsIsNow(1); | |
TestGenericAsCouldBe(1); | |
//go with the actual test | |
var limit = 100; | |
TestNonGenericAsIsNow(limit); | |
TestGenericAsCouldBe(limit); | |
Console.Read(); | |
} | |
private static void TestNonGenericAsIsNow(int limit) | |
{ | |
var sw = Stopwatch.StartNew(); | |
for (int i = 0; i < limit; i++) | |
{ | |
var fooBar = new FooBar(); | |
var barFoo = new BarFoo(); | |
var inv1 = new InvocationBarFoo(barFoo, ""); | |
var inv2 = new InvocationFooBar(fooBar, 0); | |
inv1.Invoke(); | |
inv2.Invoke(); | |
} | |
sw.Stop(); | |
Console.WriteLine(sw.Elapsed); | |
} | |
private static void TestGenericAsCouldBe(int limit) | |
{ | |
var methodFooBar = typeof(FooBar).GetMethod("Foo"); | |
var methodBarFoo = typeof(BarFoo).GetMethod("Foo"); | |
var sw = Stopwatch.StartNew(); | |
// create open instance method delegates | |
var delegateFooBar = Delegate.CreateDelegate(typeof(Func<FooBar, int, string>), methodFooBar) as Func<FooBar, int, string>; | |
var delegateBarF0o = Delegate.CreateDelegate(typeof(Func<BarFoo, string, int>), methodBarFoo) as Func<BarFoo, string, int>; | |
for (int i = 0; i < limit; i++) | |
{ | |
var fooBar = new FooBar(); | |
var barFoo = new BarFoo(); | |
var inv1 = new GenericInvocation<int, string, FooBar>(fooBar, 0, delegateFooBar); | |
var inv2 = new GenericInvocation<string, int, BarFoo>(barFoo, "", delegateBarF0o); | |
inv1.Invoke(); | |
inv2.Invoke(); | |
} | |
sw.Stop(); | |
Console.WriteLine(sw.Elapsed); | |
} | |
} | |
public interface GenInterfaceWithGenMethods<T> | |
{ | |
void DoSomething<Z>(Z z, T t); | |
} | |
public class GenInterfaceWithGenMethodsImpl<T> : GenInterfaceWithGenMethods<T> | |
{ | |
public void DoSomething<Z>(Z z, T t) | |
{ | |
} | |
} | |
public class Bar | |
{ | |
public Bar(object o, int i) | |
{ | |
throw new NotImplementedException(); | |
} | |
public object O | |
{ | |
get { throw new NotImplementedException(); } | |
} | |
} | |
public class Foo | |
{ | |
private object a; | |
private object b; | |
public object FooBar() | |
{ | |
var bar = new Bar(a ?? b, 3); | |
return bar.O; | |
} | |
private object GetB() | |
{ | |
throw new NotImplementedException(); | |
} | |
private object GetA() | |
{ | |
throw new NotImplementedException(); | |
} | |
} | |
public class InvocationFooBar | |
{ | |
private FooBar foo; | |
private string result; | |
private int param; | |
public InvocationFooBar(FooBar foo, int param) | |
{ | |
this.foo = foo; | |
} | |
public void Invoke() | |
{ | |
result = foo.Foo(param); | |
} | |
} | |
public class GenericInvocation<TIn, TOut, TTarget> | |
{ | |
private TTarget foo; | |
private TOut result; | |
private TIn param; | |
private Func<TTarget, TIn, TOut> action; | |
public GenericInvocation(TTarget target, TIn param, Func<TTarget, TIn, TOut> action) | |
{ | |
this.foo = target; | |
this.param = param; | |
this.action = action; | |
} | |
public void Invoke() | |
{ | |
result = action(foo, param); | |
} | |
} | |
public class InvocationBarFoo | |
{ | |
private BarFoo foo; | |
private int result; | |
private string param; | |
public InvocationBarFoo(BarFoo foo, string param) | |
{ | |
this.foo = foo; | |
} | |
public void Invoke() | |
{ | |
result = foo.Foo(param); | |
} | |
} | |
public class FooBar | |
{ | |
public string Foo(int i) | |
{ | |
return string.Empty; | |
} | |
} | |
public class BarFoo | |
{ | |
public int Foo(string i) | |
{ | |
return 0; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment