c#  映射对比测试(测试对象,测试案例,测试结果)

测试组件对象:

TinyMapper-EmitMapper-AutoMapper-NLiteMapper-Handwritten

对比测试案例:

类:Models

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace ConsoleApplication1
{
public class Models
{
public class Person
{
public Guid Id { get; set; }
public String Name { get; set; }
public Int32 Age { get; set; }
public Address Address { get; set; }
public string Number { get; set; }
}
public class PersonDto
{
public Guid Id { get; set; }
public String UserName { get; set; }
public Int32 Age { get; set; }
public Address Address { get; set; }
public string Number { get; set; }
} public sealed class Address
{
public string Phone { get; set; }
public string Street { get; set; }
public string ZipCode { get; set; }
}
}
}

类:CodeTimer

 using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks; namespace ConsoleApplication1
{
public sealed class CodeTimer
{
public static void Initialize()
{
Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.High;
Thread.CurrentThread.Priority = ThreadPriority.Highest;
Time("", , () => { });
} public static void Time(string name, Action action)
{
Time(name, , action);
} public static void Time(string name, int iteration, Action action)
{
if (String.IsNullOrEmpty(name)) return; // 1.
ConsoleColor currentForeColor = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(name); // 2.
GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced);
int[] gcCounts = new int[GC.MaxGeneration + ];
for (int i = ; i <= GC.MaxGeneration; i++)
{
gcCounts[i] = GC.CollectionCount(i);
} // 3.
Stopwatch watch = new Stopwatch();
watch.Start();
long cycleCount = GetCycleCount();
for (int i = ; i < iteration; i++) action();
long cpuCycles = GetCycleCount() - cycleCount;
watch.Stop(); // 4.
Console.ForegroundColor = currentForeColor;
Console.WriteLine("\tTime Elapsed:\t" + watch.ElapsedMilliseconds.ToString("N0") + "ms");
Console.WriteLine("\tCPU Cycles:\t" + cpuCycles.ToString("N0")); // 5.
for (int i = ; i <= GC.MaxGeneration; i++)
{
int count = GC.CollectionCount(i) - gcCounts[i];
Console.WriteLine("\tGen " + i + ": \t\t" + count);
} Console.WriteLine(); } private static long GetCycleCount()
{
return GetCurrentThreadTimes();
} [DllImport("kernel32.dll", SetLastError = true)]
static extern bool GetThreadTimes(IntPtr hThread, out long lpCreationTime,
out long lpExitTime, out long lpKernelTime, out long lpUserTime); private static long GetCurrentThreadTimes()
{
long l;
long kernelTime, userTimer;
GetThreadTimes(GetCurrentThread(), out l, out l, out kernelTime,
out userTimer);
return kernelTime + userTimer;
} [DllImport("kernel32.dll")]
static extern IntPtr GetCurrentThread();
}
}

类:Program

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Nelibur.ObjectMapper;//安装TinyMapper
using Nelibur.ObjectMapper.Bindings;
using EmitMapper;//安装EmitMapper
using AutoMapper;//安装AutoMapper
using NLite;
using EmitMapper.MappingConfiguration;
using System.Reflection;//安装NLite namespace ConsoleApplication1
{
public class Program : Models
{
protected static List<Person> _person;
protected static List<PersonDto> _personDto; static void Main(string[] args)
{
Base();
//进行测试 测试次数1次
CodeTimer.Time("--TinyMapper--测试", , () => Test1());
CodeTimer.Time("--EmitMapper--测试", , () => Test2());
CodeTimer.Time("--AutoMapper--测试", , () => Test3());
CodeTimer.Time("--NLiteMapper--测试", , () => Test4());
CodeTimer.Time("--Handwritten--测试", , () => Test5());
ConsoleColor currentForeColor = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("任务完成");
Console.ReadKey();
} #region 初始化数据
public static void Base()
{
_person = new List<Person>(); for (int i = ; i < ; i++)
{
Person _per = new Person()
{
Id = Guid.NewGuid(),
Name = "老黑",
Age = ,
Address = new Address
{
Phone = "",
Street = "小红门",
ZipCode = "邮编未知",
},
Number = ""
};
_person.Add(_per);
}
}
#endregion #region TinyMapper映射
static void Test1()
{
//TinyMapper.Bind<Person,PersonDto>();
//_personDto = TinyMapper.Map<List<PersonDto>>(_person);
TinyMapper.Bind<Person, PersonDto>(config =>
{
config.Bind(x => x.Id, y => y.Id);
config.Bind(x => x.Name, y => y.UserName);
config.Bind(x => x.Age, y => y.Age);
config.Bind(x => x.Address.Phone, y => y.Address.Phone);
config.Bind(x => x.Address.Street, y => y.Address.Street);
config.Bind(x => x.Address.ZipCode, y => y.Address.ZipCode);
config.Bind(x => x.Number, y => y.Number);
});
_personDto = TinyMapper.Map<List<PersonDto>>(_person);
}
#endregion #region EmitMapper 映射
static void Test2()
{
_personDto.Clear();
//EmitMapper.ObjectsMapper<List<Person>, List<PersonDto>> mapper = ObjectMapperManager.DefaultInstance.GetMapper<List<Person>, List<PersonDto>>();
//_personDto = mapper.Map(_person);
EmitMapper.ObjectsMapper<List<Person>, List<PersonDto>> mapper;
mapper = ObjectMapperManager.DefaultInstance.GetMapper<List<Person>, List<PersonDto>>(new DefaultMapConfig()
.ConvertUsing<Person, PersonDto>(value => new PersonDto
{
Id = value.Id,
UserName = value.Name,
Age = value.Age,
Address = new Address()
{
Phone = value.Address.Phone,
Street = value.Address.Street,
ZipCode = value.Address.ZipCode,
},
Number = value.Number
})
);
_personDto = mapper.Map(_person);
}
#endregion #region AutoMapper 映射
static void Test3()
{
_personDto.Clear();
//AutoMapper.Mapper.CreateMap<Person,PersonDto>();
//_personDto = AutoMapper.Mapper.Map<List<PersonDto>>(_person); AutoMapper.Mapper.CreateMap<Person, PersonDto>()
.ConstructUsing(value => new PersonDto
{
Id = value.Id,
UserName = value.Name,
Age = value.Age,
Address = new Address()
{
Phone = value.Address.Phone,
Street = value.Address.Street,
ZipCode = value.Address.ZipCode,
},
Number = value.Number
});
_personDto = AutoMapper.Mapper.Map<List<PersonDto>>(_person);
}
#endregion #region NLiteMapper 映射
static void Test4()
{
_personDto.Clear();
//NLite.Mapping.IMapper<List<Person>, List<PersonDto>> mapper = NLite.Mapper.CreateMapper<List<Person>, List<PersonDto>>();
//_personDto = mapper.Map(_person);
NLite.Mapping.IMapper<List<Person>, List<PersonDto>> mapper;
mapper = NLite.Mapper.CreateMapper<List<Person>, List<PersonDto>>()
.ConvertUsing<Person, PersonDto>(v => new PersonDto
{
Id = v.Id,
UserName = v.Name,
Age = v.Age,
Address = new Address()
{
Phone = v.Address.Phone,
Street = v.Address.Street,
ZipCode = v.Address.ZipCode,
},
Number = v.Number
});
_personDto = mapper.Map(_person);
}
#endregion
#region Handwritten 手工映射
static void Test5()
{
_personDto.Clear();
_personDto = new List<PersonDto>();
PersonDto p = new PersonDto();
for (int i = ; i < _person.Count; i++)
{
p.Id = _person[i].Id;
p.UserName = _person[i].Name;
p.Age = _person[i].Age;
p.Address = _person[i].Address;
p.Number = _person[i].Number;
_personDto.Add(p);
}
}
#endregion

对比测试结果截图:

c# 映射对比测试的更多相关文章

  1. Springboot中以配置类方式自定义Mybatis的配置规则(如开启驼峰映射等)

    什么是自定义Mybatis的配置规则? 答:即原来在mybatis配置文件中中我们配置到<settings>标签中的内容,如下第6-10行内容: 1 <?xml version=&q ...

  2. Hibernatel框架关联映射

    Hibernatel框架关联映射 Hibernate程序执行流程: 1.集合映射 需求:网络购物时,用户购买商品,填写地址 每个用户会有不确定的地址数目,或者只有一个或者有很多.这个时候不能把每条地址 ...

  3. hibernate多对多关联映射

    关联是类(类的实例)之间的关系,表示有意义和值得关注的连接. 本系列将介绍Hibernate中主要的几种关联映射 Hibernate一对一主键单向关联Hibernate一对一主键双向关联Hiberna ...

  4. Dapper逆天入门~强类型,动态类型,多映射,多返回值,增删改查+存储过程+事物案例演示

    Dapper的牛逼就不扯蛋了,答应群友做个入门Demo的,现有园友需要,那么公开分享一下: 完整Demo:http://pan.baidu.com/s/1i3TcEzj 注 意 事 项:http:// ...

  5. ElasticSearch 5学习(9)——映射和分析(string类型废弃)

    在ElasticSearch中,存入文档的内容类似于传统数据每个字段一样,都会有一个指定的属性,为了能够把日期字段处理成日期,把数字字段处理成数字,把字符串字段处理成字符串值,Elasticsearc ...

  6. .NET平台开源项目速览(14)最快的对象映射组件Tiny Mapper

    好久没有写文章,工作甚忙,但每日还是关注.NET领域的开源项目.五一休息,放松了一下之后,今天就给大家介绍一个轻量级的对象映射工具Tiny Mapper:号称是.NET平台最快的对象映射组件.那就一起 ...

  7. ASP.NET Core的路由[1]:注册URL模式与HttpHandler的映射关系

    ASP.NET Core的路由是通过一个类型为RouterMiddleware的中间件来实现的.如果我们将最终处理HTTP请求的组件称为HttpHandler,那么RouterMiddleware中间 ...

  8. mybatis_映射查询

    一.一对一映射查询: 第一种方式(手动映射):借助resultType属性,定义专门的pojo类作为输出类型,其中该po类中封装了查询结果集中所有的字段.此方法较为简单,企业中使用普遍. <!- ...

  9. 问题记录:EntityFramework 一对一关系映射

    EntityFramework 一对一关系映射有很多种,比如主键作为关联,配置比较简单,示例代码: public class Teacher { public int Id { get; set; } ...

随机推荐

  1. C#_基础

    1.形参与实参 形参是函数定义时的参数,实参是函数被引用时传给它的参数 2.重载与重写 重载:发生在同一个类中,函数(方法)名相同但参数列表必须不同,返回类型可以不同 重写:发生在继承类之间,子类必须 ...

  2. as3 中文转拼音

    private static const PinYin:Object = {"a":"\u554a\u963f\u9515","ai":&q ...

  3. 同上! 下拉复选框 点击当前的checkbox 选中后面li 添加到指定区域

    (function() { $(".cxbtntj").click(function(){ console.log($("#jsLi1").attr(" ...

  4. 关于CSS三列Float布局任务

    任务目标 掌握HTML/CSS布局的概念 掌握盒模型的概念 掌握position与float的概念以及在布局时的用法 任务描述 使用 HTML 与 CSS 按照示意图;实现三栏式布局. 左右两栏宽度固 ...

  5. SharePoint SC "Audit Settings"功能与CSOM的对应

    博客地址:http://blog.csdn.net/FoxDave SharePoint网站集中有个关于审计的功能:"Site collection audit settings&quo ...

  6. mybatis学习

    什么是 MyBatis ? MyBatis 是支持定制化 SQL.存储过程以及高级映射的优秀的持久层框架.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集.MyBatis ...

  7. nexus7 一代 手动刷4.4.4

    跟上一篇类似,但是中间出了点问题,提示说command write 出错,最后解决方法是电脑上换了个usb口插入....

  8. 【转】C#多线程示例

    using System; using System.Threading; namespace ConsoleThread { class ThreadApp { static int interva ...

  9. 用HTML和CSS实现WWDC 2015上的动画效果

    用HTML和CSS实现WWDC 2015上的动画效果 动画效果WWDC 2015   原文:https://cssanimation.rocks/wwdc15/ 译者:周晓楷(@Helkyle) 每年 ...

  10. mtk开发

    mtk套接字所有的声明放在soc_api.h 条件编译命令最常见的形式为: ? 1 2 3 4 5 #ifdef标识符 //程序段1 #else //程序段2 #endif 它的作用是:当标识符已经被 ...