Skip to content

Commit cca3d86

Browse files
author
jayt
committed
Changed unit test for reflection to ignore lookup time on each invocation although may not be necessarily reflective of usage it's a harder metric to compare agains.
1 parent 6ef55ec commit cca3d86

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

UnitTestImpromptuInterface/SpeedTest.cs

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ public void TestSetTimed()
2727
var tSetValue = "1";
2828

2929
var tWatch = TimeIt.Go(() => Impromptu.InvokeSet(tPoco, "Prop1", tSetValue), 500000);
30-
var tWatch2 = TimeIt.Go(() => tPoco.GetType().GetProperty("Prop1").SetValue(tPoco, tSetValue, new object[] { }), 500000);
30+
var tPropertyInfo = tPoco.GetType().GetProperty("Prop1");
31+
var tWatch2 = TimeIt.Go(() => tPropertyInfo.SetValue(tPoco, tSetValue, new object[] { }), 500000);
3132

3233
Console.WriteLine("Impromptu: " + tWatch.Elapsed);
3334
Console.WriteLine("Refelection: " + tWatch2.Elapsed);
@@ -48,7 +49,12 @@ public void TestPropStaticGetValueTimed()
4849

4950

5051
var tWatch = TimeIt.Go(() => { var tOut = Impromptu.InvokeGet(tAnon, "Test"); }, 500000);
51-
var tWatch2 = TimeIt.Go(() => { var tOut = tAnon.GetType().GetProperty("Test").GetValue(tAnon, null); }, 500000);
52+
53+
var tPropertyInfo = tAnon.GetType().GetProperty("Test");
54+
var tWatch2 = TimeIt.Go(() =>
55+
{
56+
var tOut = tPropertyInfo.GetValue(tAnon, null);
57+
}, 500000);
5258

5359
Console.WriteLine("Impromptu: " + tWatch.Elapsed);
5460
Console.WriteLine("Refelection: " + tWatch2.Elapsed);
@@ -67,7 +73,11 @@ public void TestMethodStaticGetValueTimed()
6773

6874

6975
var tWatch = TimeIt.Go(() => { var tOut = Impromptu.InvokeMember(tValue, "ToString"); }, 500000);
70-
var tWatch2 = TimeIt.Go(() => { var tOut = tValue.GetType().GetMethod("ToString", new Type[] { }).Invoke(tValue, new object[] { }); }, 500000);
76+
var tMethodInfo = tValue.GetType().GetMethod("ToString", new Type[] { });
77+
var tWatch2 = TimeIt.Go(() =>
78+
{
79+
var tOut = tMethodInfo.Invoke(tValue, new object[] { });
80+
}, 500000);
7181

7282
Console.WriteLine("Impromptu: " + tWatch.Elapsed);
7383
Console.WriteLine("Refelection: " + tWatch2.Elapsed);
@@ -84,7 +94,11 @@ public void TestMethodStaticGetValue4argsTimed()
8494

8595

8696
var tWatch = TimeIt.Go(() => { var tOut = Impromptu.InvokeMember(tValue, "IndexOf", "45", 0, 14, StringComparison.InvariantCulture); }, 500000);
87-
var tWatch2 = TimeIt.Go(() => { var tOut = tValue.GetType().GetMethod("IndexOf", new Type[] {typeof(string), typeof(int),typeof(int),typeof(StringComparison) }).Invoke(tValue, new object[] { "45", 0, 14, StringComparison.InvariantCulture }); }, 500000);
97+
var tMethodInfo = tValue.GetType().GetMethod("IndexOf", new Type[] { typeof(string), typeof(int), typeof(int), typeof(StringComparison) });
98+
var tWatch2 = TimeIt.Go(() =>
99+
{
100+
var tOut = tMethodInfo.Invoke(tValue, new object[] { "45", 0, 14, StringComparison.InvariantCulture });
101+
}, 500000);
88102

89103
Console.WriteLine("Impromptu: " + tWatch.Elapsed);
90104
Console.WriteLine("Refelection: " + tWatch2.Elapsed);
@@ -101,7 +115,8 @@ public void TestMethodStaticVoidTimed()
101115

102116

103117
var tWatch = TimeIt.Go(() => Impromptu.InvokeMemberAction(tValue, "Clear"), 500000);
104-
var tWatch2 = TimeIt.Go(() => tValue.GetType().GetMethod("Clear", new Type[] { }).Invoke(tValue, new object[] { }), 500000);
118+
var tMethodInfo = tValue.GetType().GetMethod("Clear", new Type[] { });
119+
var tWatch2 = TimeIt.Go(() => tMethodInfo.Invoke(tValue, new object[] { }), 500000);
105120

106121
Console.WriteLine("Impromptu: " + tWatch.Elapsed);
107122
Console.WriteLine("Refelection: " + tWatch2.Elapsed);

0 commit comments

Comments
 (0)