zoukankan      html  css  js  c++  java
  • Net反射效率(转载)

     
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Threading;
    using System.Reflection;
     

    namespace TestAssembly
    {
        public class TestClass : ITestInterface
        {
            public TestClass() { }
            public double TestMethod(double param)
            {
                return param * 0.75;
            }
        }
     
        public interface ITestInterface
        {
            double TestMethod(double param);
        }
     
        class Program
        {
            public static void Main(string[] args)
            {
                int n = 1000;
                Test1(n);//直接调用
                Test2(n);//通过InvokeMember调用
                Test3(n);//通过接口调用
                Test4(n);//绑定至delegate
                Console.In.ReadLine();
            }
     

            public delegate double TestDelegate(double param);
     
            //首先,写代码测量直接运行的效率,代码如下:
            static void Test1(int n)
            {
                TestClass tc = new TestClass();
                DateTime startTime = DateTime.Now;
                for (int i = 0; i < n; i++)
                    for (int j = 0; j < n; j++)
                        tc.TestMethod(1.0);
                TimeSpan ts = DateTime.Now - startTime;
                Console.Out.WriteLine("Test1: " + ts);
            }
     
            //接着是通过InvokeMember调用,代码如下:
            static void Test2(int n)
            {
                Type testType = typeof(TestClass);
                object obj = testType.InvokeMember(null,BindingFlags.CreateInstance, null, null, null);
                DateTime startTime = DateTime.Now;
                for (int i = 0; i < n; i++)
                    for (int j = 0; j < n; j++)
                        testType.InvokeMember("TestMethod", BindingFlags.InvokeMethod, null, obj, new object[] { 1.0 });
                TimeSpan ts = DateTime.Now - startTime;
                Console.Out.WriteLine("Test2: " + ts);
            }
     
            //然后,是将获得的object用接口来引用,然后调用方法,代码如下:
            static void Test3(int n)
            {
                Type testType = typeof(TestClass);
     
                object obj = testType.InvokeMember(null,BindingFlags.CreateInstance, null, null, null);
     
                ITestInterface instance = (ITestInterface)obj;
     
                DateTime startTime = DateTime.Now;
                for (int i = 0; i < n; i++)
                    for (int j = 0; j < n; j++)
                        instance.TestMethod(1.0);
                TimeSpan ts = DateTime.Now - startTime;
                Console.Out.WriteLine("Test3: " + ts);
            }
     

            static void Test4(int n)
            {
                Type testType = typeof(TestClass);
                object obj = testType.InvokeMember(null,BindingFlags.CreateInstance, null, null, null);
     
                TestDelegate testMethod = (TestDelegate)Delegate.CreateDelegate(typeof(TestDelegate), obj, "TestMethod");
     
                DateTime startTime = DateTime.Now;
                for (int i = 0; i < n; i++)
                    for (int j = 0; j < n; j++)
                        testMethod(1.0);
                TimeSpan ts = DateTime.Now - startTime;
                Console.Out.WriteLine("Test4: " + ts);
            }
        }
    }
     
    Test1,Test2,Test3,Test4比较效果。
  • 相关阅读:
    MySQL-安装mysql8
    MySQL-Prometheus
    MySQL-sysbench
    MySQL-客户端登录问题
    学习进度第十六周
    学习进度第十五周
    寻找最长单词链
    用户体验评价
    学习进度第十四周
    找水王问题
  • 原文地址:https://www.cnblogs.com/chinaagan/p/reflector.html
Copyright © 2011-2022 走看看