c# 映射对比测试
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# 映射对比测试的更多相关文章
- Springboot中以配置类方式自定义Mybatis的配置规则(如开启驼峰映射等)
什么是自定义Mybatis的配置规则? 答:即原来在mybatis配置文件中中我们配置到<settings>标签中的内容,如下第6-10行内容: 1 <?xml version=&q ...
- Hibernatel框架关联映射
Hibernatel框架关联映射 Hibernate程序执行流程: 1.集合映射 需求:网络购物时,用户购买商品,填写地址 每个用户会有不确定的地址数目,或者只有一个或者有很多.这个时候不能把每条地址 ...
- hibernate多对多关联映射
关联是类(类的实例)之间的关系,表示有意义和值得关注的连接. 本系列将介绍Hibernate中主要的几种关联映射 Hibernate一对一主键单向关联Hibernate一对一主键双向关联Hiberna ...
- Dapper逆天入门~强类型,动态类型,多映射,多返回值,增删改查+存储过程+事物案例演示
Dapper的牛逼就不扯蛋了,答应群友做个入门Demo的,现有园友需要,那么公开分享一下: 完整Demo:http://pan.baidu.com/s/1i3TcEzj 注 意 事 项:http:// ...
- ElasticSearch 5学习(9)——映射和分析(string类型废弃)
在ElasticSearch中,存入文档的内容类似于传统数据每个字段一样,都会有一个指定的属性,为了能够把日期字段处理成日期,把数字字段处理成数字,把字符串字段处理成字符串值,Elasticsearc ...
- .NET平台开源项目速览(14)最快的对象映射组件Tiny Mapper
好久没有写文章,工作甚忙,但每日还是关注.NET领域的开源项目.五一休息,放松了一下之后,今天就给大家介绍一个轻量级的对象映射工具Tiny Mapper:号称是.NET平台最快的对象映射组件.那就一起 ...
- ASP.NET Core的路由[1]:注册URL模式与HttpHandler的映射关系
ASP.NET Core的路由是通过一个类型为RouterMiddleware的中间件来实现的.如果我们将最终处理HTTP请求的组件称为HttpHandler,那么RouterMiddleware中间 ...
- mybatis_映射查询
一.一对一映射查询: 第一种方式(手动映射):借助resultType属性,定义专门的pojo类作为输出类型,其中该po类中封装了查询结果集中所有的字段.此方法较为简单,企业中使用普遍. <!- ...
- 问题记录:EntityFramework 一对一关系映射
EntityFramework 一对一关系映射有很多种,比如主键作为关联,配置比较简单,示例代码: public class Teacher { public int Id { get; set; } ...
随机推荐
- 工作偶遇小bug
1.下面这个在,调试一个单片机定时器是遇到的,整了一上午才找到,晕! 编译器是不认为它是错误的,所以,就你怎么运行都不是你想要结果。 2.使用pic单片机过程中,使用AD口时,相应端口的方向必须设为输 ...
- 常用的CSS定位,XPath定位和JPath定位
CSS定位 举例 描述 div#menu id为menu的div元素 div.action-btn.ok-btn class为action-btn和ok-btn的div元素 table#emailLi ...
- C++线程中packaged_tack
packaged_tack<>函数是一个你可以先定义好任务的类型,但是不想马上启动.函数可以在你想要启动任务是启动,你只需要调用你声明的函数就可以. #include <future ...
- ACM_1001_Exponentiation 详解
参考:http://blog.csdn.net/rually/article/details/8585268 #include<iostream> using namespace std; ...
- 一个Java线程小例子(仿火车票售卖)
public class MyThread extends Thread{ private static int ticket=100; public void run(){ for(int i=0; ...
- 黑马程序员——【Java高新技术】——案例:银行业务调度系统
---------- android培训.java培训.期待与您交流! ---------- 一.银行业务调度系统需求 Ø 银行内有6个业务窗口,1 - 4号窗口为普通窗口,5号窗口为快速窗口,6号窗 ...
- esxi 6 添加硬盘、网卡
添加硬盘 esxi系统装完之后,直接再接上一块硬盘,然后再使用管理工具添加硬盘 打开VMware vSphere Client,登录esxi服务器, 打开配置-存储器-选择添加存储器 选择磁盘 这里能 ...
- Echarts 动态折线图
<script src="http://echarts.baidu.com/build/dist/echarts-all.js"></script>< ...
- git入门及上传项目到github
Git入门: 如果你完全没有接触过Git,你现在只需要理解通过Git的语法(敲入一些命令)就可以将代码上传到远程的仓库或者下载到本地的仓库(服务器),我们此时应该有两个仓库,就是两个放代码 ...
- iredmail安装脚本分析(一)---iRedmail.sh
iredmail是一套以postfix为核心的整合邮件系统的安装脚本,可以达到快速部署邮件服务器的目的.为了让自己不遗忘shell的语法,所以闲来无事,学习一下他的代码. 我从官网下载他的最新版,解压 ...