c# 映射对比测试

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

测试组件对象:

TinyMapper-EmitMapper-AutoMapper-NLiteMapper-Handwritten

对比测试案例:

类:Models

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6
 7 namespace ConsoleApplication1
 8 {
 9    public  class Models
10     {
11         public class Person
12         {
13             public Guid Id { get; set; }
14             public String Name { get; set; }
15             public Int32 Age { get; set; }
16             public Address Address { get; set; }
17             public string Number { get; set; }
18         }
19         public class PersonDto
20         {
21             public Guid Id { get; set; }
22             public String UserName { get; set; }
23             public Int32 Age { get; set; }
24             public Address Address { get; set; }
25             public string Number { get; set; }
26         }
27
28         public sealed class Address
29         {
30             public string Phone { get; set; }
31             public string Street { get; set; }
32             public string ZipCode { get; set; }
33         }
34     }
35 }

类:CodeTimer

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Diagnostics;
 4 using System.Linq;
 5 using System.Runtime.InteropServices;
 6 using System.Text;
 7 using System.Threading;
 8 using System.Threading.Tasks;
 9
10 namespace ConsoleApplication1
11 {
12     public sealed class CodeTimer
13     {
14         public static void Initialize()
15         {
16             Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.High;
17             Thread.CurrentThread.Priority = ThreadPriority.Highest;
18             Time("", 1, () => { });
19         }
20
21         public static void Time(string name, Action action)
22         {
23             Time(name, 1, action);
24         }
25
26         public static void Time(string name, int iteration, Action action)
27         {
28             if (String.IsNullOrEmpty(name)) return;
29
30             // 1.
31             ConsoleColor currentForeColor = Console.ForegroundColor;
32             Console.ForegroundColor = ConsoleColor.Red;
33             Console.WriteLine(name);
34
35             // 2.
36             GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced);
37             int[] gcCounts = new int[GC.MaxGeneration + 1];
38             for (int i = 0; i <= GC.MaxGeneration; i++)
39             {
40                 gcCounts[i] = GC.CollectionCount(i);
41             }
42
43             // 3.
44             Stopwatch watch = new Stopwatch();
45             watch.Start();
46             long cycleCount = GetCycleCount();
47             for (int i = 0; i < iteration; i++) action();
48             long cpuCycles = GetCycleCount() - cycleCount;
49             watch.Stop();
50
51             // 4.
52             Console.ForegroundColor = currentForeColor;
53             Console.WriteLine("\tTime Elapsed:\t" + watch.ElapsedMilliseconds.ToString("N0") + "ms");
54             Console.WriteLine("\tCPU Cycles:\t" + cpuCycles.ToString("N0"));
55
56             // 5.
57             for (int i = 0; i <= GC.MaxGeneration; i++)
58             {
59                 int count = GC.CollectionCount(i) - gcCounts[i];
60                 Console.WriteLine("\tGen " + i + ": \t\t" + count);
61             }
62
63             Console.WriteLine();
64
65         }
66
67         private static long GetCycleCount()
68         {
69             return GetCurrentThreadTimes();
70         }
71
72         [DllImport("kernel32.dll", SetLastError = true)]
73         static extern bool GetThreadTimes(IntPtr hThread, out long lpCreationTime,
74            out long lpExitTime, out long lpKernelTime, out long lpUserTime);
75
76         private static long GetCurrentThreadTimes()
77         {
78             long l;
79             long kernelTime, userTimer;
80             GetThreadTimes(GetCurrentThread(), out l, out l, out kernelTime,
81                out userTimer);
82             return kernelTime + userTimer;
83         }
84
85         [DllImport("kernel32.dll")]
86         static extern IntPtr GetCurrentThread();
87     }
88 }

类:Program

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 using System.Threading.Tasks;
  6 using Nelibur.ObjectMapper;//安装TinyMapper
  7 using Nelibur.ObjectMapper.Bindings;
  8 using EmitMapper;//安装EmitMapper
  9 using AutoMapper;//安装AutoMapper
 10 using NLite;
 11 using EmitMapper.MappingConfiguration;
 12 using System.Reflection;//安装NLite
 13
 14 namespace ConsoleApplication1
 15 {
 16     public class Program : Models
 17     {
 18         protected static List<Person> _person;
 19         protected static List<PersonDto> _personDto;
 20
 21         static void Main(string[] args)
 22         {
 23             Base();
 24             //进行测试 测试次数1次
 25             CodeTimer.Time("--TinyMapper--测试", 1, () => Test1());
 26             CodeTimer.Time("--EmitMapper--测试", 1, () => Test2());
 27             CodeTimer.Time("--AutoMapper--测试", 1, () => Test3());
 28             CodeTimer.Time("--NLiteMapper--测试", 1, () => Test4());
 29             CodeTimer.Time("--Handwritten--测试", 1, () => Test5());
 30             ConsoleColor currentForeColor = Console.ForegroundColor;
 31             Console.ForegroundColor = ConsoleColor.Yellow;
 32             Console.WriteLine("任务完成");
 33             Console.ReadKey();
 34         }
 35
 36         #region 初始化数据
 37         public static void Base()
 38         {
 39             _person = new List<Person>();
 40
 41             for (int i = 0; i < 1000000; i++)
 42             {
 43                 Person _per = new Person()
 44                 {
 45                     Id = Guid.NewGuid(),
 46                     Name = "老黑",
 47                     Age = 22,
 48                     Address = new Address
 49                     {
 50                         Phone = "18772871387",
 51                         Street = "小红门",
 52                         ZipCode = "邮编未知",
 53                     },
 54                     Number = "123456789"
 55                 };
 56                 _person.Add(_per);
 57             }
 58         }
 59         #endregion
 60
 61         #region TinyMapper映射
 62         static void Test1()
 63         {
 64             //TinyMapper.Bind<Person,PersonDto>();
 65             //_personDto = TinyMapper.Map<List<PersonDto>>(_person);
 66             TinyMapper.Bind<Person, PersonDto>(config =>
 67             {
 68                 config.Bind(x => x.Id, y => y.Id);
 69                 config.Bind(x => x.Name, y => y.UserName);
 70                 config.Bind(x => x.Age, y => y.Age);
 71                 config.Bind(x => x.Address.Phone, y => y.Address.Phone);
 72                 config.Bind(x => x.Address.Street, y => y.Address.Street);
 73                 config.Bind(x => x.Address.ZipCode, y => y.Address.ZipCode);
 74                 config.Bind(x => x.Number, y => y.Number);
 75             });
 76             _personDto = TinyMapper.Map<List<PersonDto>>(_person);
 77         }
 78         #endregion
 79
 80         #region EmitMapper 映射
 81         static void Test2()
 82         {
 83             _personDto.Clear();
 84             //EmitMapper.ObjectsMapper<List<Person>, List<PersonDto>> mapper = ObjectMapperManager.DefaultInstance.GetMapper<List<Person>, List<PersonDto>>();
 85             //_personDto = mapper.Map(_person);
 86             EmitMapper.ObjectsMapper<List<Person>, List<PersonDto>> mapper;
 87             mapper = ObjectMapperManager.DefaultInstance.GetMapper<List<Person>, List<PersonDto>>(new DefaultMapConfig()
 88                .ConvertUsing<Person, PersonDto>(value => new PersonDto
 89                {
 90                    Id = value.Id,
 91                    UserName = value.Name,
 92                    Age = value.Age,
 93                    Address = new Address()
 94                    {
 95                        Phone = value.Address.Phone,
 96                        Street = value.Address.Street,
 97                        ZipCode = value.Address.ZipCode,
 98                    },
 99                    Number = value.Number
100                })
101                 );
102             _personDto = mapper.Map(_person);
103         }
104         #endregion
105
106         #region AutoMapper 映射
107         static void Test3()
108         {
109             _personDto.Clear();
110             //AutoMapper.Mapper.CreateMap<Person,PersonDto>();
111             //_personDto = AutoMapper.Mapper.Map<List<PersonDto>>(_person);
112
113
114             AutoMapper.Mapper.CreateMap<Person, PersonDto>()
115                 .ConstructUsing(value => new PersonDto
116                 {
117                     Id = value.Id,
118                     UserName = value.Name,
119                     Age = value.Age,
120                     Address = new Address()
121                     {
122                         Phone = value.Address.Phone,
123                         Street = value.Address.Street,
124                         ZipCode = value.Address.ZipCode,
125                     },
126                     Number = value.Number
127                 });
128             _personDto = AutoMapper.Mapper.Map<List<PersonDto>>(_person);
129         }
130         #endregion
131
132         #region NLiteMapper 映射
133         static void Test4()
134         {
135             _personDto.Clear();
136             //NLite.Mapping.IMapper<List<Person>, List<PersonDto>> mapper = NLite.Mapper.CreateMapper<List<Person>, List<PersonDto>>();
137             //_personDto = mapper.Map(_person);
138             NLite.Mapping.IMapper<List<Person>, List<PersonDto>> mapper;
139             mapper = NLite.Mapper.CreateMapper<List<Person>, List<PersonDto>>()
140             .ConvertUsing<Person, PersonDto>(v => new PersonDto
141             {
142                 Id = v.Id,
143                 UserName = v.Name,
144                 Age = v.Age,
145                 Address = new Address()
146                 {
147                     Phone = v.Address.Phone,
148                     Street = v.Address.Street,
149                     ZipCode = v.Address.ZipCode,
150                 },
151                 Number = v.Number
152             });
153             _personDto = mapper.Map(_person);
154         }
155         #endregion
158         #region Handwritten 手工映射
159         static void Test5()
160         {
161             _personDto.Clear();
162             _personDto = new List<PersonDto>();
163             PersonDto p = new PersonDto();
164             for (int i = 0; i < _person.Count; i++)
165             {
166                 p.Id = _person[i].Id;
167                 p.UserName = _person[i].Name;
168                 p.Age = _person[i].Age;
169                 p.Address = _person[i].Address;
170                 p.Number = _person[i].Number;
171                 _personDto.Add(p);
172             }
173         }
174         #endregion

对比测试结果截图:

时间: 2024-10-08 22:08:40

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

MyBatis学习(四)XML配置文件之SQL映射的XML文件

SQL映射文件常用的元素: 1.select 查询语句是MyBatis最常用的语句之一. 执行简单查询的select元素是非常简单的: <select id="selectUser" parameterType="int" resultType="hashmap"> SELECT * FROM PERSON WHERE ID = #{id} </select> 这个语句被称作selectUser,接受一个int类型的参数,

MyBatis框架中Mapper映射配置的使用及原理解析(七) MapperProxy,MapperProxyFactory

从上文<MyBatis框架中Mapper映射配置的使用及原理解析(六) MapperRegistry> 中我们知道DefaultSqlSession的getMapper方法,最后是通过MapperRegistry对象获得Mapper实例: public <T> T getMapper(Class<T> type, SqlSession sqlSession) { final MapperProxyFactory<T> mapperProxyFactory =

Hibernate的七种映射关系之七种关联映射(二)

继续上篇博客 七.Hibernate双向一对多关联映射:让多的一端来维护关系. 主要是解决一对多单向关联的缺陷,而不是需求驱动的. 1.在Student.java实体类里添加Classes引用.private Classes classes; 2.Student.hbm.xml里添加many-to-one标签:<many-to-one name="classes" column="classesid"/>.Classes.hbm.xml在例子(六)里的那

Hibernate的七种映射关系之七种关联映射(一)

关联映射就是将关联关系映射到数据库里,在对象模型中就是一个或多个引用. 一.Hibernate多对一关联映射:就是在"多"的一端加外键,指向"一"的一端. 比如多个学生对应一个班级,多个用户对应一个级别等等,都是多对一关系. 1."多"端实体加入引用"一"端实体的变量及getter,setter方法. 比如说多个学生对应一个班级,在学生实体类加入:private Grade grade; 2."多"端配置文

AD 脚本kixtart运用之一 ( 网络盘自动映射)

首先我们在Active Directory 用户和计算机工具中,在用户的配置文件下的登陆脚本里输入如下 然后我们在域共享\\nccn.int\NETLOGON\ 下新建一个NEO.bat的文件内容如下 -------------------------------------------- @echo off cmd /c %logonserver%\netlogon\KIX32.EXE %logonserver%\netlogon\kixtart.kix exit --------------

jsp映射为其他地址上去 ???

在web.xml 里面配置servlet不起作用,所以配置jsp,然后在jsp里面跳转到servlet去   ???  第八天中的<08-jsp常用标签.avi> 在web.xml里面先设定 14.jsp 为默认首页,然后在14.jsp 里面设定跳转到servlet 代码里面去 映射到其他地址去:

mybatis之高级结果映射

先贴一句官方文档内容 如果世界总是这么简单就好了. 正如官方文档所说:如果一切都是这么简单,那该多好啊,但是实际上,我们面对的是复杂的对象,就是对象里有对象,有列表对象,总之五花八门的对象.这个时候我们期盼这mybatis能帮我们来解决这个问题. 今天,我就遇到了这样的一个问题: 在做基于RBAC权限分配模型中,一共有五张表 sys_permission  记录权限信息,包括权限名称,权限url等 sys_role 记录角色信息,包括角色名和角色id sys_role_permission 记录

组策略 之 驱动器映射

目的:用户登陆自动挂载预设的网络驱动器. 可以根据不同的用户组来分类,例如:财务的共享文件夹,人资的共享文件夹. 在文件服务器共享设置好权限的文件夹然后使用策略发布给相应的用户或者组. 策略位置:用户配置---首选项---windows设置----驱动器映射 一.选择 "驱动器映射"  "新建" 说明:1."操作"选择:"更新",正常情况下选择此选项. 2.位置    :可直接属于共享文件夹的位置,例如:\\dc01\shar

iptables -对一IP映射

实现外部IP 124.3.3.3 一对一映射到内部服务器 10.0.0.8 网关:eth0 124.42.60.109 eth1 10.0.0.254 首先在路由器网关上绑定 124.3.3.3,可以是别名或辅助IP的方式 -A PREROUTING -d 124.3.3.3 -j DNAT --to-destination 10.0.0.8 -A POSTROUTING -s 10.0.0.8 -o eth0 -j SNAT --to-source 124.3.3.3 还需要添加一条,当内网访