C# 直接调用vs 委托vs动态调用vs动态类型vs反射,最佳性能测试
懒得解释,自己看代码
测试结果:
Direct call:00:00:00.0742191
Delegate Direct:00:00:00.0687487
Method Factory(IL):00:00:00.2015243
Direct IL:00:00:00.1980190
New Type:00:00:00.0860233
Reflection:00:00:02.4178550
using System.Reflection; using System.Reflection.Emit; using MoeCard.Emit; using MoeCard.Reflection; namespace MoeCard.TestConsole { public class Program { public static void DoSomeThing() { } private static void Main(string[] args) { const int loops = 10000000; Stopwatch sw = new Stopwatch(); //General delegation Action action1 = new Action(DoSomeThing); //Dynamic method MethodFactory<Action> method = MethodFactory.Create<Action>();//自己写的一个动态方法封装类,不必追究其原理 method.CallMethod(action1).Return(); Action action2 = method.Delegation; //Reflection MethodInfo doSomething = action1.Method; //Standard Dynamic method DynamicMethod method2 = new DynamicMethod("DoSomeThing", null, null); ILGenerator generator = method2.GetILGenerator(); generator.Emit(OpCodes.Call, doSomething); generator.Emit(OpCodes.Ret); Action action3 = method2.CreateDelegate(typeof (Action)) as Action; //Type Builder AssemblyName assemblyName = Assembly.GetExecutingAssembly().GetName();//最后这玩意儿胜出 AssemblyBuilder assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.RunAndSave); ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule("DynamicClassModule"); TypeBuilder newTypeBuilder = moduleBuilder.DefineType("NewType"); MethodBuilder methodBuilder = newTypeBuilder.DefineMethod("DoSomeThing", MethodAttributes.Public | MethodAttributes.Static); generator = methodBuilder.GetILGenerator(); generator.Emit(OpCodes.Call, doSomething); generator.Emit(OpCodes.Ret); Type newType = newTypeBuilder.CreateType(); Action action4 = Delegate.CreateDelegate(typeof (Action), newType.GetMethod("DoSomeThing")) as Action; assemblyBuilder.Save("DynamicAssembly.dll"); sw.Restart(); for (int i = loops; i != 0; i--) { DoSomeThing(); } sw.Stop(); Console.WriteLine("Direct call:" + sw.Elapsed); action1(); sw.Restart(); for (int i = loops; i != 0; i--) { action1(); } sw.Stop(); Console.WriteLine("Delegate Direct:" + sw.Elapsed); action2(); sw.Restart(); for (int i = loops; i != 0; i--) { action2(); } sw.Stop(); Console.WriteLine("Method Factory(IL):" + sw.Elapsed); action3(); sw.Restart(); for (int i = loops; i != 0; i--) { action3(); } sw.Stop(); Console.WriteLine("Direct IL:" + sw.Elapsed); action4(); sw.Restart(); for (int i = loops; i != 0; i--) { action4(); } sw.Stop(); Console.WriteLine("New Type:" + sw.Elapsed); sw.Restart(); for (int i = loops; i != 0; i--) { doSomething.Invoke(null, null); } sw.Stop(); Console.WriteLine("Reflection:" + sw.Elapsed); Console.ReadLine(); } } }