首页 > 移动平台 > 详细

DotNet Core 2.2下Mapper 测试 传统映射 vs EmitMapper vs AutoMapper

时间:2019-09-23 19:27:04      阅读:182      评论:0      收藏:0      [点我收藏+]

Nuget 下版本

技术分享图片

测试代码

using System;
using AutoMapper;
using System.Diagnostics;
using EmitMapper;

namespace test
{
    class Program
    {
        static void Main(string[] args)
        {
            var student = new Student
            {
                Name= "test",
                No = "1234567896766666666666666666666666666666666666666666",
            };
            var watch = new Stopwatch();
            int count = 10000000;

 
            Console.WriteLine();
            Console.WriteLine("Automapper 映射测试");
            MapperConfiguration configuration = new MapperConfiguration(
                cfg =>
                {
                    cfg.CreateMap<Student, StudentDto>();
                });
            var mapper = configuration.CreateMapper();
            watch.Start();
            for (int i = 0; i < count; ++i)
            {
                var studentdto = mapper.Map<Student, StudentDto>(student);
            }
            watch.Stop();
            Console.WriteLine($"时间消耗:{watch.ElapsedMilliseconds}ms");

            Console.WriteLine();
            Console.WriteLine("EmitMapper 映射测试");
            ObjectsMapper<Student, StudentDto> emitMap = ObjectMapperManager.DefaultInstance.GetMapper<Student, StudentDto>();
            watch.Restart();
            for (int i = 0; i < count; ++i)
            {
                StudentDto studentdt = emitMap.Map(student);
            }
            watch.Stop();
            Console.WriteLine($"时间消耗:{watch.ElapsedMilliseconds}ms");

            Console.WriteLine("\n传统方法普通映射");
            watch.Restart();
            for (int i = 0; i < count; ++i)
            {
                var studentDto = new StudentDto
                {
                    Name = student.Name,
                    No = student.No,
                };
            }
            watch.Stop();
            Console.WriteLine($"时间消耗:{watch.ElapsedMilliseconds}ms");





        }
    }

    public class StudentDto
    {
        public string Name{ get; set; }
        public string No { get; set; }
    }

    public class Student
    {
        public string Name{ get; set; }
        public string No { get; set; }
    }
}

测试结果

技术分享图片

速度对比:
AutoMapper ~ 10倍 传统映射
EmitMapper ~ 1.5倍 传统映射

PS:不考虑系统栈等等其他开销的情况下

DotNet Core 2.2下Mapper 测试 传统映射 vs EmitMapper vs AutoMapper

原文:https://www.cnblogs.com/minskiter/p/11573800.html

(1)
(1)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!