反射工具类copyProperties的基本性能粗略对比

        因为接手的项目中含有一些beanUtil类,业务代码中也不乏一些使用,但是用的工具不统一,出于好奇,我写了个小demo测试一下这些工具类的性能,这里主要挑了两个代表。

一个是org.springframework.beans.BeanUtils

另一个是:cn.hutool.core.bean

下面贴出我的测试方法:

    private void timePrint(Runnable... runnable){
        IntStream.range(0,runnable.length).forEach(index->{
            long l = System.currentTimeMillis();
            runnable[index].run();
            System.out.println("Array"+index+":"+(System.currentTimeMillis()-l));
        });
    }

    private void fore(int forCount, Consumer<Integer> consumer){
        IntStream.range(0,forCount).forEach(consumer::accept);
    }

尽管不太精确,但是也可以看出个大概(忽略jit等等其他因素的影响)

1.首先看看创建对象的性能对比,这里以创建10000个对象为例子,通过无参构造创建对比。

    @Test
    public void T9(){
        int forCount = 10000;
        timePrint(()->{
            //直接反射构造
            Constructor<ExcelDTOStudent> constructor;
            try {
                 constructor = ExcelDTOStudent.class.getConstructor();
            } catch (NoSuchMethodException e) {
                throw new RuntimeException(e);
            }
           fore(forCount,index->{
                try {
                    constructor.newInstance();
                } catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
                    throw new RuntimeException(e);
                }
            });
        },()-> fore(forCount,index->{
            //反射
            try {
                ExcelDTOStudent.class.newInstance();
            } catch (InstantiationException | IllegalAccessException e) {
                throw new RuntimeException(e);
            }
            //hu-tool
        }),()->fore(forCount,index-> ReflectUtil.newInstanceIfPossible(ExcelDTOStudent.class))
                //直接 new
        ,()->fore(forCount,index->new ExcelDTOStudent())
                //spring
        ,()->fore(forCount,index->BeanUtils.instantiateClass(ExcelDTOStudent.class)));
    }

各位不妨猜测一下结果,最快的当然毫无疑问是直接new创建的。

额,还是公布结果吧,跑了很多次,但是每次结果都差不多。各个方式之间差距是悬殊

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

来自远方的猪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值