一个比 AutoMapper 更快的模型映射的组件 Mapster
下面是官方的性能测试 Demo,感性的也可以去 Github 上下载。
贴出代码目的是如果后期直接从自己的博客中在线看。
using System;
using System.Collections.Generic;
using System.Diagnostics;
using AutoMapper;
using Benchmark.Classes;
using Mapster;
using FastExpressionCompiler;
using System.Linq.Expressions; namespace Benchmark
{
class Program
{
static double sAM;
static double sMP;
static double sMF;
static double sEM; static Func<LambdaExpression, Delegate> defaultCompiler = TypeAdapterConfig.GlobalSettings.Compiler; static void Main(string[] args)
{
try
{
Mapper.Initialize(cfg =>
{
cfg.CreateMap<Foo, Foo>();
cfg.CreateMap<Address, Address>();
cfg.CreateMap<Address, AddressDTO>();
cfg.CreateMap<Customer, CustomerDTO>();
});
TestSimpleTypes();
TestComplexTypes(); Console.WriteLine();
Console.WriteLine($"======================================================================================");
Console.WriteLine($"| Tests | Time (ms) | Slower than Mapster | Slower than Mapster + FEC |");
Console.WriteLine($"======================================================================================");
Console.WriteLine($"| Mapster v3.1.8 | { sMP,9} | { sMP / sMP,18:N2}X | { sMP / sMF,24:N2}X |");
Console.WriteLine($"| Mapster v3.1.8 + FEC | { sMF,9} | { sMF / sMP,18:N2}X | { sMF / sMF,24:N2}X |");
Console.WriteLine($"| Automapper v7.0.1 | { sAM,9} | { sAM / sMP,18:N2}X | { sAM / sMF,24:N2}X |");
Console.WriteLine($"| ExpressMapper v1.9.1 | { sEM,9} | { sEM / sMP,18:N2}X | { sEM / sMF,24:N2}X |");
Console.WriteLine($"======================================================================================");
Console.WriteLine();
} finally
{
Console.WriteLine("Finish");
Console.ReadLine();
}
} static void TestSimpleTypes()
{
Console.WriteLine("Test 1 : Simple Types");
Console.WriteLine("Competitors : Mapster, ExpressMapper, AutoMapper"); var foo = GetFoo(); TestSimple(foo, ); TestSimple(foo, ); TestSimple(foo, ); TestSimple(foo, ); //Console.WriteLine();
//Console.WriteLine("Automapper to Mapster ratio: " + (AutomapperTime / MapsterTime).ToString("###.00") + " X slower");
//Console.WriteLine("ExpressMapper to Mapster ratio: " + (ExpressMapperTime / MapsterTime).ToString("###.00") + " X slower");
//Console.WriteLine(); } static void TestComplexTypes()
{
Console.WriteLine();
Console.WriteLine(); Console.WriteLine("Test 2 : Complex Types");
Console.WriteLine("Competitors : Mapster, ExpressMapper, AutoMapper"); var customer = GetCustomer(); //TypeAdapterConfig.GlobalSettings.DestinationTransforms.Upsert<Guid>(x => x); //ObjectMapperManager.DefaultInstance.GetMapper<Customer, CustomerDTO>().Map(customer); Test(customer, ); Test(customer, ); Test(customer, ); Test(customer, ); Test(customer, ); //Console.WriteLine();
//Console.WriteLine("Automapper to Mapster ratio: " + (AutomapperTime/MapsterTime).ToString("###.00") + " X slower");
//Console.WriteLine("ExpressMapper to Mapster ratio: " + (ExpressMapperTime/MapsterTime).ToString("###.00") + " X slower");
//Console.WriteLine();
} static void Test(Customer item, int iterations)
{
Console.WriteLine(); Console.WriteLine("Iterations : {0}", iterations); //TestCustomerNative(item, iterations); TypeAdapterConfig.GlobalSettings.Compiler = defaultCompiler; //switch compiler
TypeAdapterConfig.GlobalSettings.Compile(typeof(Customer), typeof(CustomerDTO)); //recompile
item.Adapt<Customer, CustomerDTO>(); //exercise
TestMapsterAdapter<Customer, CustomerDTO>(item, iterations, ref sMP); TypeAdapterConfig.GlobalSettings.Compiler = exp => exp.CompileFast(); //switch compiler
TypeAdapterConfig.GlobalSettings.Compile(typeof(Customer), typeof(CustomerDTO)); //recompile
item.Adapt<Customer, CustomerDTO>(); //exercise
TestMapsterAdapter<Customer, CustomerDTO>(item, iterations, ref sMF); ExpressMapper.Mapper.Map<Customer, CustomerDTO>(item); //exercise
TestExpressMapper<Customer, CustomerDTO>(item, iterations, ref sEM); Mapper.Map<Customer, CustomerDTO>(item); //exercise
TestAutoMapper<Customer, CustomerDTO>(item, iterations, ref sAM);
} static void TestSimple(Foo item, int iterations)
{ Console.WriteLine(); Console.WriteLine("Iterations : {0}", iterations); TypeAdapterConfig.GlobalSettings.Compiler = defaultCompiler; //switch compiler
TypeAdapterConfig.GlobalSettings.Compile(typeof(Foo), typeof(Foo)); //recompile
item.Adapt<Foo, Foo>(); //exercise
TestMapsterAdapter<Foo, Foo>(item, iterations, ref sMP); TypeAdapterConfig.GlobalSettings.Compiler = exp => exp.CompileFast(); //switch compiler
TypeAdapterConfig.GlobalSettings.Compile(typeof(Foo), typeof(Foo)); //recompile
item.Adapt<Foo, Foo>(); //exercise
TestMapsterAdapter<Foo, Foo>(item, iterations, ref sMF); //TestValueInjecter<Foo, Foo>(item, iterations); ExpressMapper.Mapper.Map<Foo, Foo>(item); //exercise
TestExpressMapper<Foo, Foo>(item, iterations, ref sEM); Mapper.Map<Foo, Foo>(item); //exercise
TestAutoMapper<Foo, Foo>(item, iterations, ref sAM);
} static void TestCustomerNative(Customer item, int iterations)
{
Console.WriteLine("Handwritten Mapper:\t" + Loop<Customer>(item, get =>
{
var dto = new CustomerDTO(); dto.Id = get.Id;
dto.Name = get.Name;
dto.AddressCity = get.Address.City; dto.Address = new Address() { Id = get.Address.Id, Street = get.Address.Street, Country = get.Address.Country, City = get.Address.City }; dto.HomeAddress = new AddressDTO() { Id = get.HomeAddress.Id, Country = get.HomeAddress.Country, City = get.HomeAddress.City }; dto.Addresses = new AddressDTO[get.Addresses.Length];
for (int i = ; i < get.Addresses.Length; i++)
{
dto.Addresses[i] = new AddressDTO() { Id = get.Addresses[i].Id, Country = get.Addresses[i].Country, City = get.Addresses[i].City };
} dto.WorkAddresses = new List<AddressDTO>();
foreach (var workAddress in get.WorkAddresses)
{
dto.WorkAddresses.Add(new AddressDTO() { Id = workAddress.Id, Country = workAddress.Country, City = workAddress.City });
} }, iterations));
} static void TestMapsterAdapter<TSrc, TDest>(TSrc item, int iterations, ref double counter)
where TSrc : class
where TDest : class, new()
{
var time = Loop(item, get => get.Adapt<TSrc, TDest>(), iterations);
Console.WriteLine("Mapster:\t\t" + time);
counter += time;
} static void TestExpressMapper<TSrc, TDest>(TSrc item, int iterations, ref double counter)
where TSrc : class
where TDest : class, new()
{
var time = Loop(item, get => ExpressMapper.Mapper.Map<TSrc, TDest>(get), iterations);
Console.WriteLine("ExpressMapper:\t\t" + time);
counter += time;
} static void TestAutoMapper<TSrc, TDest>(TSrc item, int iterations, ref double counter)
where TSrc : class
where TDest : class, new()
{
//if (iterations > 50000)
// Console.WriteLine("AutoMapper still working please wait..."); var time = Loop(item, get => Mapper.Map<TSrc, TDest>(get), iterations);
Console.WriteLine("AutoMapper:\t\t" + time);
counter += time;
} static long Loop<T>(T item, Action<T> action, int iterations = )
{
return Time(item, a =>
{
for (int i = ; i < iterations; i++)
{
action(a);
}
});
} static long Time<T>(T item, Action<T> action)
{
var sw = new Stopwatch();
sw.Start();
action(item);
sw.Stop();
return sw.ElapsedMilliseconds;
} #region Data static Customer GetCustomer()
{
Customer c = new Customer()
{
Address = new Address() { City = "istanbul", Country = "turkey", Id = , Street = "istiklal cad." },
HomeAddress = new Address() { City = "istanbul", Country = "turkey", Id = , Street = "istiklal cad." },
Id = ,
Name = "Eduardo Najera",
Credit = 234.7m,
WorkAddresses = new List<Address>()
{
new Address() {City = "istanbul", Country = "turkey", Id = , Street = "istiklal cad."},
new Address() {City = "izmir", Country = "turkey", Id = , Street = "konak"}
},
Addresses = new List<Address>()
{
new Address() {City = "istanbul", Country = "turkey", Id = , Street = "istiklal cad."},
new Address() {City = "izmir", Country = "turkey", Id = , Street = "konak"}
}.ToArray()
}; return c;
} static Foo GetFoo()
{
var o = new Foo
{
Name = "foo",
Int32 = ,
Int64 = ,
NullInt = ,
DateTime = DateTime.Now,
Doublen = ,
Foo1 = new Foo { Name = "foo one" },
Foos = new List<Foo>
{
new Foo {Name = "j1", Int64 = , NullInt = },
new Foo {Name = "j2", Int32 = , NullInt = },
new Foo {Name = "j3", Int32 = , NullInt = },
},
FooArr = new[]
{
new Foo {Name = "a1"},
new Foo {Name = "a2"},
new Foo {Name = "a3"},
},
IntArr = new[] { , , , , },
Ints = new[] { , , },
}; return o;
} #endregion }
}
谢谢浏览!
一个比 AutoMapper 更快的模型映射的组件 Mapster的更多相关文章
- 一个比NPM更快更安全可靠的JavaScript包管理工具——Yarn
yarn安装: npm intall -g yarn 查看安装是否成功: yarn -v yarn常用的命令以及和npm的对照如下: 更详细的请查看官方文档
- linux 下程序员专用搜索源码用来替代grep的软件ack(后来发现一个更快的: ag), 且有vim插件的
发现一个比ack更快更好用的: https://github.com/ggreer/the_silver_searcher , 使用时命令为ag,它是基于ack的代码二次开发的,所有使用方法基本 ...
- 比NGINX更快:nginx-1.15.5 vs mongols-1.2.3
nginx是多进程web服务器的优秀代表. 本文要用mongols-1.2.3实现一个比nginx更快的多进程的web服务器. mongols是C++ 服务器基础设施库, 它的主要特性如下: tcp ...
- 修复bug有哪些更快的技术?做好这6点就够了
你有没有想过为什么有时修复错误似乎比它应该花费更长的时间?当你终于找到问题时,事实证明你所需要的只是一个小小的改变.然而,花了很多时间才能找到正在发生的事情.这种情况比我想象的更频繁. 另一方面,当您 ...
- MindSpore模型精度调优实战:如何更快定位精度问题
摘要:为大家梳理了针对常见精度问题的调试调优指南,将以"MindSpore模型精度调优实战"系列文章的形式分享出来,帮助大家轻松定位精度问题,快速优化模型精度. 本文分享自华为云社 ...
- EdgeFormer: 向视觉 Transformer 学习,构建一个比 MobileViT 更好更快的卷积网络
前言 本文主要探究了轻量模型的设计.通过使用 Vision Transformer 的优势来改进卷积网络,从而获得更好的性能. 欢迎关注公众号CV技术指南,专注于计算机视觉的技术总结.最新技术跟 ...
- android开发者您还在为模拟器犯愁吗?神级android模拟器---Genymotion一个更快、接近完美的模拟器……
摘要:Android系统非常特别,App须要进行模拟化測试.即使这样仍然有解决的办法---虚拟化技术. 之前的模拟器比方eclipse自带的是非常慢的一种,并且模拟器的版本号并非最新的.开机.能够说差 ...
- LSM树——LSM 将B+树等结构昂贵的随机IO变的更快,而代价就是读操作要处理大量的索引文件(sstable)而不是一个,另外还是一些IO被合并操作消耗。
Basic Compaction 为了保持LSM的读操作相对较快,维护并减少sstable文件的个数是很重要的,所以让我们更深入的看一下合并操作.这个过程有一点儿像一般垃圾回收算法. 当一定数量的ss ...
- ZeroMQ一个更小、更快、更简单的智能传输层协议
这个githube上的教程是非常好的,是个中文翻译,大家直接学这个就行 https://github.com/anjuke/zguide-cn/tree/master/bin 原文地址: https: ...
随机推荐
- 一次业务网关用ASP.NET Core 2.1重构的小结
目录 前言 统一鉴权 服务限流 路由转发 参数重组 链路跟踪 熔断降级 服务计次 业务指标监控 日志记录 迭代更新 总结 前言 对于API网关,业界貌似对它进行下划分,有下面几个分类/场景. 面向We ...
- CLRCore(CLR核心机制)
JIT--第一次--标记已--存根--调用--查找存根--执行机器码 C#和CIL的关系: C#和N#都是CIL实现,但是彼此不能互通: C#和N#公开不分满足规范,我们才能互通 CLS就是描述多语言 ...
- Vue中的组件及路由使用
1.组件是什么 组件系统是 Vue 的一个重要概念,因为它是一种抽象,允许我们使用小型.独立和通常可复用的组件构建大型应用.通常一个应用会以一棵嵌套的组件树的形式来组织: 1.1组件的声 ...
- Spring MVC的常用注解(一)
概述 Spring从2.5版本开始引入注解,虽然版本不断变化,但是注解的特性一直被延续下来并不断进行扩展,这里就来记录一下Spring MVC中常用的注解,本文记录@Controller.@Reque ...
- SSM框架之Mybatis(3)dao层开发
Mybatis(3)dao层开发 以实现类完成CRUD操作 1.持久层dao层接口的书写 src\main\java\dao\IUserDao.java package dao; import dom ...
- java报错问题记录
java.lang.NoSuchMethodError 运行时错误,再编译期一般不会出现这个问题.NoSuchMethodError中文意思是没有找到方法,遇到这个错误并不是说依赖的jar包.方法不存 ...
- 【转载】Android开发中巧用Activity和Fragment
1.Activity的生命周期 1)多个Activity组成Activity栈,当前活动位于栈顶.我们先来看看各种Activity基类的类图: 当Activity类定义出来之后,这个Activity何 ...
- [b0025] vmware_桥接网路设置
总结: 桥接 模式上外网比较简单. 缺点是每次重启虚拟机,分配的IP可能变化,导致 xshell 连接 时都要修改,很麻烦 桥接模式能不能配置静态IP ? 1. 环境 物理机网络 情况,从外部自动 ...
- 详解YUV数据格式
我们在讲 FFmpeg 系列的时候,有提到 YUV 的.其中包括YUV播放器.简单的YUV格式介绍. 一.YUV简介 YUV,是一种颜色编码方法.常使用在各个影像处理元件中. YUV在对照片或影片编码 ...
- Could not resolve resource location pattern [classpath:com/****/mappers/*.xml]: class path resource [com/****/mappers/] cannot be resolved to URL because it does not exist的问题
这里建议先去看看路径的问题,看看application.xml的里面的导入的相应的配置文件的路径有没有问题,如下: 再者看看相应的注解有没有加上,service和controller等的注解 如果再不 ...