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; } ...
随机推荐
- Python 3.x 使用csv模块写入数据
with open(fileName,'w',newline='') as f: self.fileNames = ['timestamp','elapsedtime'] writer = csv.D ...
- NPOI 2.0 创建Excel文件
如果只是简单的处理的话,只需要引用下载压缩包里的 NPOI.dll (office 2003)或 NPOI.OOXML.dll (office 2007) 文件而已. using System; us ...
- php session的操作
[设置session数据] <?php session_start(); //初始化 //session文件中可以保存 dobule,integer,bool,array,object $_SE ...
- ObjC宏定义小细节
Macros A definition that takes arguments, particularly more than one, is often known as a macro: #de ...
- entity framework 新手入门篇(3)-entity framework实现orderby,count,groupby,like,in,分页等
前面我们已经学习了entityframework的基本的增删改查,今天,我们将在EF中实现一些更加贴近于实际功能的SQL方法. 承接上面的部分,我们有一个叫做House的数据库,其中包含house表和 ...
- Best Coder Round#25 1003 树的非递归访问
虽然官方解释是这题目里的树看作无向无环图,从答案来看还是在“以1作为根节点”这一前提下进行的,这棵树搭建好以后,从叶节点开始访问,一直推到根节点即可——很像动态规划的“自底向上”. 但这棵树的搭建堪忧 ...
- C#中datatable导出excel(三种方法)
方法一:(拷贝直接可以使用,适合大批量资料, 上万笔) Microsoft.Office.Interop.Excel.Application appexcel = new Microsoft.Offi ...
- android layout_weight讲解
Layout_weight是线性布局,也就是LinearLayout里面用到的,下面通过实验来看这个Layout_weight的特性. 1.当控件的属性android:layout_width=&qu ...
- Linux下查看tomcat连接数 .
netstat -na | grep ESTAB | grep 80 | wc -l 80是端口号
- esxi 6 添加硬盘、网卡
添加硬盘 esxi系统装完之后,直接再接上一块硬盘,然后再使用管理工具添加硬盘 打开VMware vSphere Client,登录esxi服务器, 打开配置-存储器-选择添加存储器 选择磁盘 这里能 ...