java bean属性快速复制

fastcopy-bean属性快速复制

github地址:https://github.com/zwk0508/fastcopy.git

原理

使用asm的字节码技术,动态生成类,实现属性的复制,生成的类,例如:

public final class Generator$1$Copier implements Copier {
    public Generator$1$Copier() {
    }

    @Override
    public void copy(Object var1, Object var2) {
        User u1 = (User) var1;
        User u2 = (User) var2;
        u2.setUserId(u2.getUserId());
    }
}

速度

  1. 比spring的BeanUtils快,快了两个数量级,初始化快了大约4~5倍
  2. 比spring的BeanCopier快一点,初始化快了大约3倍
  3. apache的就不比了

测试代码,dto需要自己复制一份

@Data
public class Attachment {

    private Integer attachId;

    private Integer projectId;

    private Integer layerId;

    private String attachName;

    private Integer attachDelStatus;

    private Integer isComplete;

    private Integer attachStatus;

    private String attachPath;

    private Double attachSize;

    private String attachEndfille;

    private String attachWebEndfile;

    private Double attachWebSize;

    private Integer showOrder;

    private String attachDoOrder;

    private String attachTrim;

    private String attachLocation;

    private String errorCode;

    private String attachTaskId;

    private Integer compTotal;

    private String attachCompPath;

    private Integer attachCompRetry;

    private String attachOsbimUrl;

    private String attachOsbUrl;

    private Double attachOsbSize;

    private Integer attachDoVersion;

    private Integer attachCategory;

    private String creator;

    private Timestamp createDate;

    private Timestamp updateDate;

    private String info;
}

测试方法:

public void test() throws Exception {
    Attachment attachment = new Attachment();
    attachment.setAttachId(1);
    attachment.setProjectId(1);
    attachment.setLayerId(1);
    attachment.setAttachName("asdf");
    attachment.setAttachDelStatus(1);
    attachment.setIsComplete(1);
    attachment.setAttachStatus(1);
    attachment.setAttachPath("aaaa");
    attachment.setAttachSize(12356D);
    attachment.setAttachEndfille("12356D");
    attachment.setAttachWebEndfile("12356D");
    attachment.setAttachWebSize(12356D);
    attachment.setShowOrder(12356);
    attachment.setAttachDoOrder("12356");
    attachment.setAttachTrim("12356");
    attachment.setAttachLocation("12356");
    attachment.setErrorCode("12356");
    attachment.setAttachTaskId("12356");
    attachment.setCompTotal(12356);
    attachment.setAttachCompPath("12356");
    attachment.setAttachCompRetry(12356);
    attachment.setAttachOsbimUrl("12356");
    attachment.setAttachOsbUrl("12356");
    attachment.setAttachOsbSize(12356D);
    attachment.setAttachDoVersion(12356);
    attachment.setAttachCategory(12356);
    attachment.setCreator("12356");
    attachment.setCreateDate(new Timestamp(System.currentTimeMillis()));
    attachment.setUpdateDate(new Timestamp(System.currentTimeMillis()));
    attachment.setInfo("info");
    AttachmentDto dto = new AttachmentDto();
    for (int i = 0; i < 100; i++) {
        long start = System.nanoTime();
        FastCopyUtil.copy(attachment, dto);
        System.out.println(System.nanoTime() - start);
    }
    BeanCopier beanCopier = null;
    for (int i = 0; i < 100; i++) {
        long start = System.nanoTime();
        if (beanCopier == null) {
            beanCopier = BeanCopier.create(Attachment.class, AttachmentDto.class, true);
        }
        Converter c = (o, aClass, o1) -> o;
        beanCopier.copy(attachment, dto, c);
        System.out.println(System.nanoTime() - start);
    }
    for (int i = 0; i < 100; i++) {
        long start = System.nanoTime();
        BeanUtils.copyProperties(attachment, dto);
        System.out.println(System.nanoTime() - start);
    }
}

统计耗时,单位是纳秒
第一行是初始化耗时

fastcopyBeanCopierBeanUtils
3066250087656500129095100
36004700616200
28003400555000
20004800497600
18006000513800
18003600428700
19003200411700
18003300401000
18004500336400
18003100318400
17003400841100
17003700655400
16004500527500
16003300733600
80003300542700
300034009791400
270033001095600
270011700501400
30003300285400
29003300335500
27003500312900
26004800338000
26003600295700
25003300277300
25003100309100
260010300296400
26003500304400
240044600237200
26003200242500
26004000711800
26003700586600
25003600247300
25003700259300
26003200231300
26004200269300
26003200209600
270034006221100
23003800921200
24004000169100
24003300156400
29003900161800
26003600178400
25004600152700
24003900144700
24003600143000
23003500160200
30004400158900
27003600528400
27003300175500
26003100179500
25003100213500
26004500332600
26003500163300
26003500141900
240035001480900
24004100654600
29003500173800
30003900134900
30003100146600
32004700148300
28003500131200
25003600132500
26003100134300
25005000132200
27003600133000
32003500138000
38003300190300
33003800131300
27004000137000
25003900163100
27003300148900
25003600153800
24004300148300
24003700146400
25004000192900
25003200158800
23003900154500
27003700149100
29003500148700
30003500189800
27003400148100
27004100173900
27003200164800
26003600143600
26003300103800
25004300107200
2500340089100
2800330089700
2700320089300
2800380092100
30003100100100
29002900109900
2600330094200
2800390084400
2700360086400
26003700133900
26004600144300
28003900121700
33004600117400
26005000121400
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值