Reflection BenchMark
For a project I have been developing, over the last week, I have come to utilize a lot of reflection, generics and their combination using MakeGenericType.
So today last day, I began to optimize its performance based on my experience of reflection and the .net world. I have used a lot of reflection so I believe my performance hunches were correct.
I assumed that
-
It is a lot quicker to have a Dictionary of Types instead of calling MakeGenericType
-
It is a lot quicker to have a dictionary of PropertyInfo instead of using repeatetly GetPropetyInfo
-
It is a lot quicker to have a dictionary of MethodInfo instead of using repeatetly GetMethodInfo
-
It is better to create static functions that will do your work as a single function, instead of calling through reflection the constructor and then the method.
So tonight I created a Benchmarking program that proved I was right on all 4. What I didn’t expect was the great time MakeGenericType needed to execute.
In the benchmark project I have a TestType.cs without generics and a GenericType.cs with two generic parameters.Each class has a public constructor, an Execute method and a static ExecuteStatic which calls the constructor and the the Execute method.
Because the operations are pretty simple, there will be no milliseconds but average ticks.
The names consist of the following
-
Test or Generic class
-
With reflection or not
-
Instantiating and calling Execute or just call static ExecuteStatic
-
Optimized or not
So here are the results
TestStatic->7
TestInstance->7
TestReflectionStatic->21
TestReflectionInstance->33
TestReflectionStaticOptimized->16
TestReflectionInstanceOptimized->24
GenericStatic->8
GenericInstance->7
GenericReflectionStatic->41
GenericReflectionInstance->75
GenericReflectionStaticOptimized->37
GenericReflectionInstanceOptimized->67
GenericReflectionMake->32
GenericReflectionMakeOptimized->12
The above example is with just one combination of generic parameters used. When I increase the number of generic combinations the results become
TestStatic->9
TestInstance->9
TestReflectionStatic->22
TestReflectionInstance->31
TestReflectionStaticOptimized->16
TestReflectionInstanceOptimized->23
GenericStatic->8
GenericInstance->9
GenericReflectionStatic->40
GenericReflectionInstance->71
GenericReflectionStaticOptimized->37
GenericReflectionInstanceOptimized->66
GenericReflectionMake->31
GenericReflectionMakeOptimized->19
As you can see the performance weakens, probably because of the dictionary and its long keys. String manipulation in .NET is not one of its strongest features.
Another thing is that in my classes, there is only one method, so minor as the difference in optimization as it may seem, keep in mind that the more methods you have in a type the slower GetMethodInfo executes.
In my benchmark I didn’t implement test about properties but I firmly believe that they will not have any difference from methods.
There is no much code in the post, because the optimization is pretty forward. Just reuse Type and MethodInfo as much as possible.
For example the not optimized way is
public static void TestReflectionInstance() { Type type = typeof(TestType); object tt = type.GetConstructor(new Type[] { }).Invoke(null); type.GetMethod(“Execute”).Invoke(tt, null); Stopwatch sw = new Stopwatch(); sw.Start(); for (int i = 0; i < iterations; i++) { type = typeof(TestType); tt = type.GetConstructor(new Type[] { }).Invoke(null); type.GetMethod(“Execute”).Invoke(tt, null); } sw.Stop(); if (Write) Console.WriteLine(“TestReflectionInstance ” + sw.ElapsedTicks.ToString()); times["TestReflectionInstance"] += sw.ElapsedTicks; }
and the optimized way is
public static void TestReflectionInstanceOptimized() { Type type = typeof(TestType); ConstructorInfo ci = type.GetConstructor(new Type[] { }); object tt = ci.Invoke(null); MethodInfo mi = type.GetMethod(“Execute”); mi.Invoke(tt, null); Stopwatch sw = new Stopwatch(); sw.Start(); for (int i = 0; i < iterations; i++) { tt = ci.Invoke(null); mi.Invoke(tt, null); //type.GetMethod(“Execute”).Invoke(tt, null); } sw.Stop(); if (Write) Console.WriteLine(“TestReflectionInstanceOptimized ” + sw.ElapsedTicks.ToString()); times["TestReflectionInstanceOptimized"] += sw.ElapsedTicks; }
My belief is that if you optimize well you code when using reflection, then the performance penalty is not so great but the advantages are many times more significant. I believe this must have been taken into account, when Microsoft developed WPF, WWF and LINQ.
The next day while I was out in the sun some thoughts came to me that made me modify the makegeneric comparicon
The Generic Benchmarking is not fair. The practical dilemma is that at any single point of execution is it better to call MakeGenericType for a generic type or to find the type form a variety of pre made types from a dictionary.
So I ran the test using the below codes
Type ii = type.MakeGenericType(new Type[] { typeof(int), typeof(int) });
and the optimized version
Type ii = makedTypes[type.FullName + "|int|int"];
and the results where which
GenericReflectionMake->26 GenericReflectionMakeOptimized->9
This shows that having pre made generic types in a sort of list is far much quicker than calling MakeGenericType
Tags: Knowledge, Performance
You can comment below, or link to this permanent URL from your own site.