• 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比较效果。
  • 相关阅读:
    C++ primer 简读
    C++函数及类方法(待补充)
    C++参数传递-复制和引用
    c++动态分配内存
    C++内存与指针
    c++循环
    C++容器vector及迭代器对象iterator
    c++字符类型一些知识点
    CNN中卷积运算转化成矩阵乘法的实现——img2col+GEMM大矩阵乘法
    046 LeetCode go
  • 原文地址:https://www.cnblogs.com/chinaagan/p/reflector.html
Copyright © 2020-2023  润新知