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; } ...
随机推荐
- javap查看class文件
通过JVM编译java文件生成class字节码文件,很多时候很想用工具打开看看,目前还不清楚哪一个软件专门查看class文件的,但是通过windows下的javap命令可以查看详细的class文件 S ...
- linux -a 到 -z 的意义
shell if判断中常用的也就是绿色部分,尾部部分越看越不懂.从百度文库转载. [ -a FILE ] 如果 FILE 存在则为真. [ -b FILE ] 如果 FILE 存在且是一个块特殊文件则 ...
- play(1) 第一个简单的应用
去年去了一家公司,公司要求要回使用play,所以在几天的时间内猛学习了一段时间play,发现play!有很多优点:简单,小巧,高开发效率,集成很多功能,容易调试.现在虽然已经不在那家公司,没有使用pl ...
- boost和std中的thread的引用参数
boost 1.60.0 先上代码: #include <boost/thread.hpp> #include <iostream> void add(int &i) ...
- Counting Bits(Difficulty: Medium)
题目: Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate ...
- 将excel文件批量转成pdf
防止数据编辑.改动带来的不一致性,常常要将excel文件转成pdf文件再共享.发送给对方.有时excel文件还挺多,手头上保存实在是太慢了.就考虑用VBA批量转置. 掌握几个东西,就比较容易了: 1. ...
- 数据存储之CoreData
#import "ViewController.h" #import <CoreData/CoreData.h> #import "Person.h" ...
- eclipse中添加python开发环境
由于自己一直使用的是eclipse这个IDE,在写spark,java等都是用它,主要是用它比较顺手,也并不是觉得它有什么特别好的之处.下面主要介绍一下,在window系统下,eclipse中搭建py ...
- android最常用的退出方法
public class AppUtils extends Application{ private List<Activity> activityList = new LinkedLis ...
- GitHub 上一份很受欢迎的前端代码优化指南
http://segmentfault.com/a/1190000002587334?utm_source=weekly&utm_medium=email&utm_campaign=e ...