反射 性能 java_Java反射性能

336da40393ff6950166911d087bb3b3e.png

喵喵时光机

您可能会发现,JVM正在优化A=新的A()。如果将对象放入数组中,则它们的性能不太好。)以下指纹.new A(), 141 ns

A.class.newInstance(), 266 nsnew A(), 103 ns

A.class.newInstance(), 261 nspublic class Run {

    private static final int RUNS = 3000000;

    public static class A {

    }

    public static void main(String[] args) throws Exception {

        doRegular();

        doReflection();

        doRegular();

        doReflection();

    }

    public static void doRegular() throws Exception {

        A[] as = new A[RUNS];

        long start = System.nanoTime();

        for (int i = 0; i 

            as[i] = new A();

        }

        System.out.printf("new A(), %,d ns%n", (System.nanoTime() - start)/RUNS);

    }

    public static void doReflection() throws Exception {

        A[] as = new A[RUNS];

        long start = System.nanoTime();

        for (int i = 0; i 

            as[i] = A.class.newInstance();

        }

        System.out.printf("A.class.newInstance(), %,d ns%n", (System.nanoTime() - start)/RUNS);

    }}这表明我的机器上的差别大约是150 ns。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值