这里只是给出性能测试,没有评判ModelMapper,项目中是否使用ModelMapper完全取决于个人
import org.modelmapper.ModelMapper;
/**
* @author lucky
*/
public class Test {
public static void main(String[] args) {
SourceModel sourceModel = new SourceModel();
sourceModel.setId(1);
sourceModel.setName("darling");
sourceModel.setEmail("123@163.com");
Address address = new Address();
address.setName("天宫市");
sourceModel.setAddress(address);
long start = System.currentTimeMillis();
// 10w 9 10 0
// setMethod(sourceModel);
// 10w 432 10 65
modelMapper(sourceModel);
long end = System.currentTimeMillis();
System.out.println("花费=======》"+(end-start));
}
private static void setMethod(SourceModel sourceModel) {
for (int i = 0; i < 100000; i++) {
TargetModel targetModel = new TargetModel();
targetModel.setName(sourceModel.getName());
targetModel.setEmail(sourceModel.getEmail());
targetModel.setAddress(sourceModel.getAddress().getName());
}
}
private static void modelMapper(SourceModel sourceModel) {
ModelMapper modelMapper = new ModelMapper();
for (int i = 0; i < 100000; i++) {
TargetModel targetModel = modelMapper.map(sourceModel, TargetModel.class);
}
}
}
生成装换10W次对象,原生的set方法花费10 毫秒,使用ModelMapper 耗费 430毫秒。
生成装换10次对象,原生的set方法花费小于1毫秒,使用ModelMapper 耗费 60毫秒。