问题描述
我已经下载并测试了这两个映射库.我编写了一个程序,该程序具有100000次迭代,并映射了同一类的bean:
I have downloaded and testing these two mapping libraries. I wrote a program which has 100000 iterations and maps the beans of the same class:
public class IntBean {
@JMap
private int int1;
@JMap
private int int2;
.
.
.
@JMap
private int int10;
}
在迭代开始之前创建映射器:
Mappers are created BEFORE iterations start:
private JMapper jmapper = new JMapper(IntBean.class, IntBean.class);
private MapperFactory orikaFactory = new DefaultMapperFactory.Builder().build();
private MapperFacade orikaFacade = null;
orikaFactory.registerClassMap(orikaFactory.classMap(IntBean.class,IntBean.class).byDefault().toClassMap());
orikaFacade = orikaFactory.getMapperFacade();
每次迭代中的内容是什么
What is in each iteration:
this.orikaFacade.map(a1, a2);
或
a2 = (A) this.jmapper2.getDestination(a1);
手势映射:1ms
Orika映射:32ms
手动映射:6毫秒速度非常快!
推土机:1140毫秒
我知道Orika和Jmapper是Google的出色库,它们使用反射的方式与Dozer不同,例如Dozer慢得多,它们以某种方式将反射方式生成代码.
I know, that Orika and Jmapper are great libraries from Google and they use reflection in a different way than for example Dozer, which is much slower, they se reflection to generete code somehow..
我有3个问题:
1)它们的工作原理-在生成代码时,在Maven构建期间,在运行时-每次在代码中创建映射器时?创建映射器时,它们是否动态更改类代码字节??
1) How they work - when the code is generated, during maven build, in runtime - everytime when I create mapper in code? Are they change class code byte dynamically when I create mappers.?
2)为什么我注意到了这种速度差异?如果以某种方式生成代码,那为什么会有不同的结果
2) Why there is this speed difference that I noticed? If the generate code somehow, then why there are different results
3)您将选择哪个库,为什么?两者具有相同的功能?为什么两者都来自Google?为什么Google不开发Orika而是创建Jmapper?
3) Which library would you choose and why? Both have the same capabilities? Why both come from Google? Why Google didnt develop Orika and created Jmapper instead?
推荐答案
我对Jmapper不熟悉,所以我将重点介绍Orika和Dozer
I'm not familiar with Jmapper, so i'll concentrate on Orika and Dozer
-
它们如何工作?他们俩的工作方式各有不同.使用反射的推土机和使用字节码生成的Orika.在Maven构建期间?什么也没发生,一切都在运行时完成.推土机通过其get方法访问字段,并通过setter方法在目标对象中设置值.Orkia生成字节码来完成工作,就好像您自己进行了手工映射一样.第一次转换的速度较慢,此后每次转换的速度都应更快.
How do they work? They both work reasonably differently. Dozer using reflection and Orika using bytecode generation. During maven build? Nothing happens, its all done at runtime. Dozer access fields via their get methods and sets the value in a target object via setter methods.Orkia generates bytecode to do the work as if you'd done a hand mapping yourself. Its slow on the first conversion and should be faster on each one after that.
推土机,应始终保持大致相同的速度,并依靠反射. Orika,字节码生成,第一次运行在生成映射代码时应该慢很多.
Dozer, should always be roughly the same speed, relies on reflection. Orika, bytecode generation, 1st run should be alot slower while its generating the mapping code.
简短的答案,要看情况.您要映射什么?如果类大致相似,则Dozer非常擅长从一种类型映射到另一种类型.它根本不处理地图.如果您的对象中有Map,请准备编写自定义转换器代码
Short answer, it depends. What are you trying to map? Dozer is very good at mapping from one type to another if the classes are roughly similar. It doesnt deal with Maps at all. Be prepared to write custom converter code if you have a Map in your Object
Orika非常擅长在相同类型的两个对象之间映射数据.它的某些List处理有点奇怪,它将列表像单个对象一样对待,而不是单个对象的集合.同样,也准备为此编写一些代码.
Orika is fantastic at mapping data between two objects of the same type. Some of its List handling is a little odd where it'll treat a list like a single object instead of a collection of individual objects. Again, be prepared to write some code for this too.
都不能很好地处理大型Object图,请准备为此编写很多配置.
Neither handles a large Object graph particularly well, be prepared to write a lot of configuration for this one.
除非您要在应用程序中进行大量映射,否则需要频繁更改的映射.自己写
Unless you're going to be doing a lot of mapping in an application, or mapping that needs to change frequently. Write your own
这篇关于Orika vs JMapper-工作原理和速度差异-为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!