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; } ...
随机推荐
- 清华微积分-1_Ch1习题
U3-1 Here are some sets: (1) R both and (2) ∅ both and (3) (1,+∞) open set (4) [−1,0] closed set, - ...
- 也来说说C/C++里的volatile关键字
去年年底的样子,何登成写了一篇关于C/C++ volatile关键字的深度剖析blog(C/C++ Volatile关键词深度剖析).全文深入分析了volatile关键字的三个特性.这里不想就已有内容 ...
- selenium配置
1.firebug安装--火狐插件 2.firepath安装--火狐插件 3.
- oracle:ORACLE 实际返回的行数超出请求的行数
写的存储过程,执行后一直报实际返回的行数超出请求的行数的错误. 原因:select prdt_id into prdt_id from.... 两个变量名称相同造成的..哎 第一个变量换成大写..问 ...
- PHP 删除文件(图片)
/** * 删除图片或文件 * @author Zhenwei Zhang <772979140@qq.com> * @param string $pic 图片或文件地址 */functi ...
- 《C++primer》v5 第4章 表达式 读书笔记 习题答案
4.1 105 4.2 *vec.begin()=*(vec.begin())//先调用点运算符,再解引用 *vec.begin()+1=(*vec.begin())+1//先解引用,再加一 4.3略 ...
- mvc 导入,导出excel
最近主要做导入excel 在网上查询了代码 public FileResult DownLoadExcelJiZuChaXunGenRenXiaoFeiJiLu() { DataTable dt = ...
- 好久没写Blog了
上一年的经历: <炸年兽>搞了一阵后,美术去创业了.. 和另一个美术断断续续,做了个<斗战圣佛>,挺山寨的,都没敢跟别人说. 不管怎么说也算是自己上了一个appStore的游戏 ...
- Xen启动过程分析(还是分享过来吧,找了好长时间)
XEN启动过程 Xen Hypervisor运行在Ring0,在启动过程中,Xen首先被引导:系统由Grub启动,遵循Multiboot引导规范:然后Linux内核做为module也被引导入 ...
- iOS--UIView和UIWindow用法
基础 UI(user interface)是用户界面:iOS的应用是由各种各样的UI控件组成 UIWindow就是一个窗口,学的第一个基础类,就是一个容器,可以在容器上放不同的内容,每个app都需要借 ...