是否非要用interface關鍵字來實現介面?

來源:互聯網
上載者:User
想法我還不能系統的表達, 先發個測試, 大家看看有沒有毛病.

委託測試:    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 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);
            }
        }
    }

類測試:    public class ClassTest {
        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
    }

測試的代碼:        [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!");
        }

    }

結果:
Class: 1050, Interface: 1802 , Func: 1008, Func In a Getter: 1525

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.