C#反射性能优化--上篇

前两天看到一篇说Automapper为什么比用反射快的文章,觉得挺有意思,反射的性能低老早就知道,但是一直没上手测过,对于反射性能优化也不知道。今天也没什么事情,想到这个让我好奇心按捺不住了,今天就写个测试一下。

目标

使用反射和Automapper组件分别实现 将对象转换成另一个对象

创建两个类

    public class PersonSource
    {
        public string Name { get; set; }
        public int Age { get; set; }
        public string Address { get; set; }
        public string City { get; set; }
        public string Region { get; set; }
        public string PostalCode { get; set; }
        public string Country { get; set; }
        public string Phone { get; set; }
        public string FatherName { get; set; }
        public string MatherName { get; set; }
        public string Fax { get; set; }
    }
    public class Person
    {
        public string Name { get; set; }
        public int Age { get; set; }
        public string Address { get; set; }
        public string City { get; set; }
        public string Region { get; set; }
        public string PostalCode { get; set; }
        public string Country { get; set; }
        public string Phone { get; set; }
        public string FatherName { get; set; }
        public string MatherName { get; set; }
        public string Fax { get; set; }
    }

使用反射进行对象的转换的类

    public class Auto
    {
        public static TDestination Mapper<TSource, TDestination>(TSource source) where TSource : class where TDestination : class
        {
            var typeSource = source.GetType();
            var typeDestination = typeof(TDestination);

            var propsSource = typeSource.GetProperties();
            var dest = Activator.CreateInstance(typeDestination);
            var propsDestination = dest.GetType().GetProperties();

            foreach (var prop in propsSource)
            {
                var obj = prop.GetValue(source);

                var propDestination = propsDestination.FirstOrDefault(t => t.Name == prop.Name);
                if (propDestination != null)
                {
                    propDestination.SetValue(dest, obj);
                }
            }

            return dest as TDestination;
        }
    }

使用BenchMark测试一下 一百万个对象的转换

代码如下:

    [MemoryDiagnoser]
    public class BenchmarkDemo
    {
        public List<PersonSource> GetList()
        {
            List<PersonSource> list = new List<PersonSource>();

            Enumerable.Range(0, 1_000_000).ToList().ForEach(x =>
            {
                PersonSource personSource = new PersonSource
                {
                    Name = "张三",
                    Age = x,
                    Address = "Beijing",
                    City = "ShangHai",
                    Region = "Huabei",
                    PostalCode = "10000",
                    Country = "China",
                    Phone = "15200329999",
                    FatherName = "老张",
                    MatherName = "零零",
                    Fax = "2200-7112-555"
                };
                list.Add(personSource);
            });

            return list;
        }

        [Benchmark]
        public void MyAutoTest()
        {
            var list = GetList();
            list.ForEach(t =>
            {
                Person destination = Auto.Mapper<PersonSource, Person>(t);
            });
        }

        [Benchmark]
        public void AutoMapperTest()
        {
            var list = GetList();

            var config = new MapperConfiguration(cfg =>
            {
                cfg.CreateMap<PersonSource, Person>();
            });

            IMapper iMapper = config.CreateMapper();

            list.ForEach(t =>
            {
                Person destination = iMapper.Map<PersonSource, Person>(t); 
            });
        }
    }
        static void Main(string[] args)
        {
            var summary = BenchmarkRunner.Run<BenchmarkDemo>();
        }

运行代码结果如下:
在这里插入图片描述
上图可以看到,在转换一百万个对象时,使用反射比使用Automapper组件耗时更多 (一百万个对象转换大概耗时反射是Automapper的10倍,一千万个对象转换时 反射是Automapper的20倍),占用的内存也更多。
下一篇我们一起探究一下Automapper是如何做到如此高性能的,看看它是用什么方式优化反射的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值