手机
当前位置:查字典教程网 >编程开发 >C语言 >C# interface与delegate效能比较的深入解析
C# interface与delegate效能比较的深入解析
摘要:前言以前在CodeComplete2nd(代码大全2)这本书上看过说在像是C#这种类型语言中能不要用delegate就尽量不要用,多使用in...

前言

以前在Code Complete 2nd(代码大全2)这本书上看过

说在像是C#这种类型语言中能不要用delegate就尽量不要用,多使用interface取代,以避免效能上的影响

实践出真理,所以我就写了个小范例来测试

我的硬件是2.66G 4核心CPU,内存4G

C# interface与delegate效能比较的深入解析1

我不知道是不是电脑比较快,以及我写的函数太小的关系

次数到了10000000次才看到有影响

C# interface与delegate效能比较的深入解析2

到了100000000次后看起来也是还好

总而分析,还是会有影响

需要高效运算或是在嵌入式中,应该还是要多注意一点

代码

复制代码 代码如下:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Diagnostics;

namespace Performance

{

class Program

{

delegate int Add(int a, int b);

static Add myDelegate;

const int LOOP_COUNT = 100000000;

static void Main(string[] args)

{

myDelegate = new Add(TestAdd);

IOrz orz = new Orz();

Stopwatch st = new Stopwatch();

st.Start();

for (int i = 0; i < LOOP_COUNT; i++)

{

int c = orz.DoIt(1, 2);

}

st.Stop();

Console.WriteLine(" Call Interface Elapsed time:{0} ms", st.ElapsedMilliseconds);

st.Reset();

st.Start();

for (int i = 0; i < LOOP_COUNT; i++)

{

int d = myDelegate(3, 5);

}

st.Stop();

Console.WriteLine("Call Delegate Elapsed time :{0} ms", st.ElapsedMilliseconds);

Console.ReadLine();

}

static int TestAdd(int a, int b)

{

int c = a + b;

return c;

}

}

}

【C# interface与delegate效能比较的深入解析】相关文章:

基于malloc与free函数的实现代码及分析

基于C++ list中erase与remove函数的使用详解

利用C语言实践OOP,以及new,delete的深入分析

基于C语言sprintf函数的深入理解

c/c++中变量的声明和定义深入解析

二叉搜索树的插入与删除(详细解析)

Eclipse对printf()不能输出到控制台的快速解决方法

c++中new的三种用法详细解析

C++ 类的静态成员深入解析

C/C++数据对齐详细解析

精品推荐
分类导航