使用dynamic来简化反射提高性能【测试运行1亿次时间对比】

using System;
using System.Diagnostics;
using System.Reflection;

namespace ReflectionVsDynamicDemo
{
    /// <summary>
    ///     https://www.cnblogs.com/LifeDecidesHappiness/p/14810810.html
    ///     使用dynamic来简化反射提高性能【测试运行1亿次时间对比】
    ///     LDH @ 2021-10-15
    /// </summary>
    internal class Program
    {
        /// <summary>
        ///     执行次数
        /// </summary>
        private static readonly int _execTimes = 10000 * 10000;

        private static void Main()
        {
            Console.Title = "使用dynamic来简化反射实现,并且提高了性能【测试运行1亿次时间对比】";

            UseReflectionCallGetName();
            UseDynamicCallGetName();

            Console.ForegroundColor = ConsoleColor.DarkGreen;
            Console.WriteLine("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");

            UseReflectionCallAddTime();
            UseDynamicCallAddTime();

            Console.WriteLine("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");

            Console.ReadKey();
        }

        /// <summary>
        ///     使用反射调用方法用时
        /// </summary>
        private static void UseReflectionCallAddTime()
        {
            var sample = new DynamicSample();
            var m = sample.GetType().GetMethod("Add");

            var watch = new Stopwatch();
            watch.Start();
            for (var i = 0; i < _execTimes; i++)
                if (m != null)
                {
                    var re = (int)m.Invoke(sample, new object[] { 1, 2 });
                }

            watch.Stop();
            Console.WriteLine($"使用反射调用方法用时:{watch.ElapsedMilliseconds}ms");
        }

        /// <summary>
        ///     使用dynamic调用方法用时
        /// </summary>
        private static void UseDynamicCallAddTime()
        {
            dynamic sample = new DynamicSample();

            var watch = new Stopwatch();
            watch.Start();
            for (var i = 0; i < _execTimes; i++)
            {
                int re = sample.Add(1, 2);
            }

            watch.Stop();
            Console.WriteLine($"使用dynamic调用方法用时:{watch.ElapsedMilliseconds}ms");
        }

        /// <summary>
        ///     用dynamic调用GetName()方法
        /// </summary>
        private static void UseDynamicCallGetName()
        {
            dynamic myInfo = new Me();
            string result = myInfo.GetName();
            Console.WriteLine($"dynamic调用GetName()方法:{result}");
        }


        /// <summary>
        ///     用反射调用GetName()方法
        /// </summary>
        private static void UseReflectionCallGetName()
        {
            var a = Assembly.GetExecutingAssembly();
            var instance = a.CreateInstance("ReflectionVsDynamicDemo.Me");
            if (instance != null)
            {
                var type = instance.GetType();
                var mi = type.GetMethod("GetName");
                if (mi != null)
                {
                    var result = mi.Invoke(instance, null);
                    Console.WriteLine($"用反射调用GetName()方法:{result}");
                }
            }
        }
    }

    internal class Me
    {
        public string Blog { get; set; }

        public string GetName()
        {
            return "LDH";
        }
    }

    public class DynamicSample
    {
        public string Name { get; set; }

        public int Add(int a, int b)
        {
            return a + b;
        }
    }
}

使用dynamic来简化反射提高性能【测试运行1亿次时间对比】

 

上一篇:computed 和 watch 的区别和运用的场景?


下一篇:2021-09-29