是否非要用interface关键字来实现接口?
想法我还不能系统的表达, 先发个测试, 大家看看有没有毛病.
委托测试:
属性包装的委托:
类测试:
接口测试:
测试的代码:
结果:
Class: 1050, Interface: 1802 , Func: 1008, Func In a Getter: 1525
委托测试:
public delegate T Func1<T, T1>(T1 t);
public class FuncTest {
public readonly Func1<long, long> Test;
public FuncTest() {
Test = Fib;
}
private long Fib(long n) {
if (n <= 1) {
return n;
}
else {
return Test(n - 1) + Test(n - 2);
}
}
}
public class FuncTest {
public readonly Func1<long, long> Test;
public FuncTest() {
Test = Fib;
}
private long Fib(long n) {
if (n <= 1) {
return n;
}
else {
return Test(n - 1) + Test(n - 2);
}
}
}
属性包装的委托:
public class FuncInAGetterTest {
private readonly Func1<long, long> _Test;
public FuncInAGetterTest() {
_Test = Fib;
}
public Func1<long, long> Test {
get {
return _Test;
}
}
private long Fib(long n) {
if (n <= 1) {
return n;
}
else {
return Test(n - 1) + Test(n - 2);
}
}
}
private readonly Func1<long, long> _Test;
public FuncInAGetterTest() {
_Test = Fib;
}
public Func1<long, long> Test {
get {
return _Test;
}
}
private long Fib(long n) {
if (n <= 1) {
return n;
}
else {
return Test(n - 1) + Test(n - 2);
}
}
}
类测试:
public class ClassTest {
public long Test(long n) {
if (n <= 1) {
return n;
}
else {
return Test(n - 1) + Test(n - 2);
}
}
}
public long Test(long n) {
if (n <= 1) {
return n;
}
else {
return Test(n - 1) + Test(n - 2);
}
}
}
接口测试:
public interface IInterfaceTest {
long Test(long n);
}
public class InterfaceTest : IInterfaceTest {
private IInterfaceTest _interface;
public InterfaceTest() {
_interface = this;
}
private long Test(long n) {
if (n <= 1) {
return n;
}
else {
return _interface.Test(n - 1) + _interface.Test(n - 2);
}
}
#region IInterfaceTest Members
long IInterfaceTest.Test(long n) {
return Test(n);
}
#endregion
}
long Test(long n);
}
public class InterfaceTest : IInterfaceTest {
private IInterfaceTest _interface;
public InterfaceTest() {
_interface = this;
}
private long Test(long n) {
if (n <= 1) {
return n;
}
else {
return _interface.Test(n - 1) + _interface.Test(n - 2);
}
}
#region IInterfaceTest Members
long IInterfaceTest.Test(long n) {
return Test(n);
}
#endregion
}
测试的代码:
[TestMethod]
public void ClassInterfaceDelegatePerformance() {
//
// TODO: Add test logic here
//
Stopwatch sw = new Stopwatch();
ClassTest classTest = new ClassTest();
IInterfaceTest interfaceTest = new InterfaceTest();
FuncTest funcTest = new FuncTest();
FuncInAGetterTest funcInAGetter = new FuncInAGetterTest();
long classResult = 0;
sw.Start();
for (int i = 0; i < 20; i++)
classResult = classTest.Test(30);
sw.Stop();
long classTime = sw.ElapsedMilliseconds;
sw.Reset();
sw.Start();
for (int i = 0; i < 20; i++)
interfaceTest.Test(30);
sw.Stop();
long interfaceTime = sw.ElapsedMilliseconds;
long funcResult = 0;
sw.Reset();
sw.Start();
for (int i = 0; i < 20; i++) {
funcResult = funcTest.Test(30);
}
sw.Stop();
long funcTime = sw.ElapsedMilliseconds;
sw.Reset();
sw.Start();
for (int i = 0; i < 20; i++)
funcInAGetter.Test(30);
sw.Stop();
Debug.WriteLine("Class: " + classTime.ToString() + ", Interface: " + interfaceTime.ToString() +
" , Func: " + funcTime.ToString() + ", Func In a Getter: " + sw.ElapsedMilliseconds.ToString());
Assert.IsTrue(classTime > funcTime, "Func is slower!");
Assert.AreEqual(classResult, funcResult, "Not Equal!");
}
}
public void ClassInterfaceDelegatePerformance() {
//
// TODO: Add test logic here
//
Stopwatch sw = new Stopwatch();
ClassTest classTest = new ClassTest();
IInterfaceTest interfaceTest = new InterfaceTest();
FuncTest funcTest = new FuncTest();
FuncInAGetterTest funcInAGetter = new FuncInAGetterTest();
long classResult = 0;
sw.Start();
for (int i = 0; i < 20; i++)
classResult = classTest.Test(30);
sw.Stop();
long classTime = sw.ElapsedMilliseconds;
sw.Reset();
sw.Start();
for (int i = 0; i < 20; i++)
interfaceTest.Test(30);
sw.Stop();
long interfaceTime = sw.ElapsedMilliseconds;
long funcResult = 0;
sw.Reset();
sw.Start();
for (int i = 0; i < 20; i++) {
funcResult = funcTest.Test(30);
}
sw.Stop();
long funcTime = sw.ElapsedMilliseconds;
sw.Reset();
sw.Start();
for (int i = 0; i < 20; i++)
funcInAGetter.Test(30);
sw.Stop();
Debug.WriteLine("Class: " + classTime.ToString() + ", Interface: " + interfaceTime.ToString() +
" , Func: " + funcTime.ToString() + ", Func In a Getter: " + sw.ElapsedMilliseconds.ToString());
Assert.IsTrue(classTime > funcTime, "Func is slower!");
Assert.AreEqual(classResult, funcResult, "Not Equal!");
}
}
结果:
Class: 1050, Interface: 1802 , Func: 1008, Func In a Getter: 1525