懒得解释,自己看代码

测试结果:

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();
}
}
}

  

C# 直接调用vs 委托vs动态调用vs动态类型vs反射,最佳性能测试的更多相关文章

  1. 反射-优化及程序集等(用委托的方式调用需要反射调用的方法(或者属性、字段),而不去使用Invoke方法)

    反射-优化及程序集等(用委托的方式调用需要反射调用的方法(或者属性.字段),而不去使用Invoke方法)   创建Delegate (1).Delegate.CreateDelegate(Type, ...

  2. C#动态调用C++编写的DLL函数

    C#动态调用C++编写的DLL函数 动态加载DLL需要使用Windows API函数:LoadLibrary.GetProcAddress以及FreeLibrary.我们可以使用DllImport在C ...

  3. C++调用DLL有两种方法——静态调用和动态调用

    C++调用DLL有两种方法——静态调用和动态调用 标签: dllc++winapinullc 2011-09-09 09:49 11609人阅读 评论(0) 收藏 举报  分类: cpp(30)  [ ...

  4. 【C#】 使用Gsof.Native 动态调用 C动态库

    [C#] 使用Gsof.Native 动态调用 C动态库 一.背景 使用C# 开发客户端时候,我们经常会调用一些标准的动态库或是C的类库.虽然C# 提供的PInvoke的方式,但因为使用的场景的多变, ...

  5. 采用异步来实现重新连接服务器或者重新启动服务 C#中类的属性的获取 SignalR2简易数据看板演示 C#动态调用泛型类、泛型方法 asp .net core Get raw request. 从壹开始前后端分离[.NetCore 不定期更新] 38 ║自动初始化数据库

    采用异步来实现重新连接服务器或者重新启动服务 开启异步监听,不会导致主线程的堵塞,在服务异常断开后一直检测重新连接服务,成功连接服务后通知各个注册的客户端! #region 检测断线并重连OPC服务 ...

  6. C#中动态调用DLL动态链接库

    其中要使用两个未公开的Win32 API函数来存取控制台窗口,这就需要使用动态调用的方法,动态调用中使用的Windows API函数主要有三个,即:Loadlibrary,GetProcAddress ...

  7. C#中动态调用DLL动态链接库(转)

    本来是想实现控制台程序运行时自动全屏,但是只找到VC下的实现方法(http://www.vckbase.com/bbs/prime/viewprime.asp?id=347). 其中要使用两个未公开的 ...

  8. 托管非托管Dll动态调用

    原文:托管非托管Dll动态调用 最近经常看到有人问托管非托管Dll调用的问题.对于动态库的调用其实很简单.网上很多代码都实现了Dll的静态调用方法.我主要谈论下动态库的动态加载. 对于托管动态库,实现 ...

  9. C#使用表达式树动态调用方法并实现99乘法表

    我们在使用C#编程的时候,经常使用反射来动态调用方法,但有时候需要动态的生成方法,下面介绍使用表达式树的方式来自动生成方法,并调用. 首先需要说明什么是表达式,熟悉Linq的程序猿都用过类似于下面的代 ...

随机推荐

  1. 遗传算法之GAUL

    遗传算法之GAUL简介 简介        GAUL(遗传算法工具库的简称) GAUL is an open source programming library, released under th ...

  2. [字符编码]Invalid byte 1 of 1-byte UTF-8 sequence终极解决方案

    今天在eclipse中编写pom.xml文件时,注释中的中文被eclipse识别到错误:Invalid byte 1 of 1-byte UTF-8 sequence,曾多次遇到该问题,问题的根源是: ...

  3. js-我理解的闭包

    一:什么是闭包 <JS高级程序设计>指出:闭包是指有有权访问另一个函数作用域中变量的函数. 二:闭包的使用 闭包的常见的创建方式是 子函数嵌套在父函数的内部,这样,子函数就可以访问父函数中 ...

  4. C入门---位运算

    程序中的所有数在计算机内存中都是以二进制的形式储存的.位运算直接对整数在内存中的二进制位进行操作.由于位运算直接对内存数据进行操作,不需要转成十进制,因此处理速度非常快. (1),与(&)运算 ...

  5. 2013 acm 长沙网络赛 G题 素数+枚举 Goldbach

    题目 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3856 先预处理求出两个素数的和与积,然后枚举n-prime和n/pr ...

  6. WCF use ProtoBuf

    ProtoBuf, 比起xml和json, 传输的数据里面没有自描述标签, 而且是基于二进制的, 所以有着超高的传输效率, 据牛人张善友的描述, 可以替代WCF的自带的编码方案, 效率有极大的提升. ...

  7. C#实现自动单击

    最新玩了一下上学时候玩的游戏,但游戏里面变化太多了,进去后等级就很高,要不停地点击鼠标加技能. 所以利用工作中常用的C#调用 API不停地点击鼠标. 如图: 为方便在基础上修改,我把整个解决方案放到百 ...

  8. iOS所有的子视图

    for (id view in [self.view subviews]) { if ([view isEqual:[UITextField class]]) { NSLog(@"你想要?& ...

  9. c#在主窗体panel 容器内嵌入另一个窗体(子窗体)的实现

    主窗体:  子窗体: 把子窗体嵌入到主窗体的panel 右侧中: 代码: { public MainForm() { InitializeComponent(); } private void Clo ...

  10. Python中实现异步并发查询数据库

    这周又填了一个以前挖下的坑. 这个博客系统使用Psycopy库实现与PostgreSQL数据库的通信.前期,只是泛泛地了解了一下SQL语言,然后就胡乱拼凑出这么一个简易博客系统. 10月份找到工作以后 ...