C# interface與delegate效能比擬的深刻解析。本站提示廣大學習愛好者:(C# interface與delegate效能比擬的深刻解析)文章只能為提供參考,不一定能成為您想要的結果。以下是C# interface與delegate效能比擬的深刻解析正文
我不曉得是否是電腦比擬快,和我寫的函數太小的關系
次數到了10000000次才看到有影響
到了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;
}
}
}