예제 #1
0
        public void MapSinglePerformanceTest()
        {
            try
            {
                var times  = 1000000;
                var source = new Source {
                    Name = "1", Age = 1, Birth = DateTime.Now, NoMatch1 = "noMatchField", Week = DayOfWeek.Wednesday
                };
                var target = new Target();

                //MapperConfiguration cfg = new MapperConfiguration(c =>
                //{
                //    c.CreateMap<Source, Target>();
                //});
                //var autoMap = cfg.CreateMapper();


                //Stopwatch autoMapWatch = Stopwatch.StartNew();
                //for (int i = 0; i < times; i++)
                //{
                //    autoMap.Map(source, target, typeof(Source), typeof(Target));
                //}
                //autoMapWatch.Stop();



                var       sourceType      = typeof(Source);
                var       targetType      = typeof(Target);
                Stopwatch expressionWatch = Stopwatch.StartNew();
                var       mapAction       = EasyMap.GetMapAction <Source, Target>();
                for (int i = 0; i < times; i++)
                {
                    mapAction(source, target, null);// 最快,比automap快5倍
                    //EasyMap.Map(source, target,null); // 和automap差不多
                    //EasyMap.Map(sourceType, targetType, source, target, null); // 为automap的5倍时间
                }
                expressionWatch.Stop();
            }
            catch (Exception ex)
            {
                var a = ex;
            }
        }