惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
The Blog of Author Tim Ferriss
S
Schneier on Security
博客园 - 聂微东
爱范儿
爱范儿
大猫的无限游戏
大猫的无限游戏
有赞技术团队
有赞技术团队
腾讯CDC
博客园 - 叶小钗
WordPress大学
WordPress大学
博客园_首页
J
Java Code Geeks
Last Week in AI
Last Week in AI
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
V
V2EX
Microsoft Azure Blog
Microsoft Azure Blog
The GitHub Blog
The GitHub Blog
N
Netflix TechBlog - Medium
Y
Y Combinator Blog
Schneier on Security
Schneier on Security
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Recorded Future
Recorded Future
The Register - Security
The Register - Security
C
Cybersecurity and Infrastructure Security Agency CISA
P
Privacy & Cybersecurity Law Blog
P
Proofpoint News Feed
P
Privacy International News Feed
K
Kaspersky official blog
C
CERT Recently Published Vulnerability Notes
阮一峰的网络日志
阮一峰的网络日志
F
Full Disclosure
NISL@THU
NISL@THU
AWS News Blog
AWS News Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
U
Unit 42
MongoDB | Blog
MongoDB | Blog
A
Arctic Wolf
云风的 BLOG
云风的 BLOG
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
D
Darknet – Hacking Tools, Hacker News & Cyber Security
T
Threatpost
D
Docker
人人都是产品经理
人人都是产品经理
T
Tailwind CSS Blog
V2EX - 技术
V2EX - 技术
G
GRAHAM CLULEY
M
MIT News - Artificial intelligence
H
Heimdal Security Blog
N
News and Events Feed by Topic
P
Proofpoint News Feed

博客园 - Rickie

DeepSeek-V3 解读:优化效率与规模 一步一步构建RAG智能问答系统 Milvus向量数据库入门实践 LangChain轻松入门和开发实践 Hugging Face 轻松入门 PyTorch深度学习零基础入门 Spring Security OAuth2+JWT开发实践 Spring Security开发实践 Redis 7.x 入门和开发实战 RedisInsight :Redis 官方可视化工具 Hadoop v3.1 大数据技术快速入门 《Apache RocketMQ 深入浅出》系列文章 Kafka v2.3 快速入门与实践 Apache Doris 轻松入门和快速实践 一步一步编译最新版Apache Doris 0.15版本的详细过程 COLA 4.x和DDD项目实践精粹 阿里开源COLA 4.0源码编译和部署过程 阿里DDD项目最佳实践-COLA 架构总览 Java实体映射工具MapStruct详解
Java实体映射工具MapStruct 与BeanUtils性能比较
Rickie · 2021-10-04 · via 博客园 - Rickie

本文通过一个简单的示例代码,比较MapStruct和BeanUtils的性能数据,实测一下性能到底有多大的差距。关于MapStruct工具的详细介绍可以参考《Java实体映射工具MapStruct详解》技术专栏,提供完整示例项目代码下载。

MapStruct属于在编译期,生成调用get/set方法进行赋值的代码,生成对应的Java文件。在编译期间消耗少许的时间,换取运行时的高性能。

一、创建测试应用

如图所示,创建测试应用performance-test,用于测试StudentDto对象和Student对象之间的转换。

其中基于MapStruct工具开发的StudentMapper映射接口的代码如下所示:

@Mapper(componentModel = "spring")

public interface StudentMapper {

    StudentMapper INSTANCE = Mappers.getMapper(StudentMapper.class);

    @Mapping(target = "className", source= "className")

    Student getModelFromDto(StudentDto studentDto);

    @Mapping(target = "className", source = "className")

    //@InheritInverseConfiguration(name = "getModelFromDto")

    StudentDto getDtoFromModel(Student student);

}

二、测试代码

分别通过MapStruct 和 BeanUtils 将相同对象转换100W次,看看整体的耗时数据。测试类PerformanceTest的代码如下所示:

public class PerformanceTest {

    public static void main(String[] args) {

        for(int i=0; i<5; i++) {

            Long startTime = System.currentTimeMillis();

            for(int count = 0; count<1000000; count++) {

                StudentDto studentDto = new StudentDto();

                studentDto.setId(count);

                studentDto.setName("Java实体映射工具MapStruct详解");

                studentDto.setClassName("清华大学一年级");

                studentDto.setCreateTime(new Date());

                Student student = new Student();

                BeanUtils.copyProperties(studentDto, student);

            }

            System.out.println("BeanUtils 100w次实体映射耗时:" + (System.currentTimeMillis() - startTime));

            startTime = System.currentTimeMillis();

            for(int count = 0; count<1000000; count++) {

                StudentDto studentDto = new StudentDto();

                studentDto.setId(count);

                studentDto.setName("Java实体映射工具MapStruct详解");

                studentDto.setClassName("清华大学一年级");

                studentDto.setCreateTime(new Date());

                Student student = StudentMapper.INSTANCE.getModelFromDto(studentDto);

            }

            System.out.println("MapStruct 100w次实体映射耗时:" + (System.currentTimeMillis()-startTime));

            System.out.println();

        }

    }

}

输出结果如下所示:

BeanUtils 100w次实体映射耗时:548

MapStruct 100w次实体映射耗时:33

BeanUtils 100w次实体映射耗时:231

MapStruct 100w次实体映射耗时:35

BeanUtils 100w次实体映射耗时:227

MapStruct 100w次实体映射耗时:27

BeanUtils 100w次实体映射耗时:219

MapStruct 100w次实体映射耗时:29

BeanUtils 100w次实体映射耗时:218

MapStruct 100w次实体映射耗时:28

从测试结果上可以看出,MapStruct的性能基本上优于BeanUtils一个数量级。