手机
当前位置:查字典教程网 >编程开发 >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效能比较的深入解析】相关文章:

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

new和malloc的区别深入解析

C++中的三种继承public,protected,private详细解析

虚函数与纯虚函数(C++与Java虚函数的区别)的深入分析

C++中栈结构建立与操作详细解析

C字符串操作函数的实现详细解析

iostream与iostream.h的区别详细解析

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

C++中函数的默认参数详细解析

C++实现strcmp字符串比较的深入探讨

精品推荐
分类导航