effective java 取消不必要的对象创建

求和

 private static long sum() {
        //如果使用Long 则需要自动拆箱操作
        long sum = 0L;
        for (long i = 0L; i <= Integer.MAX_VALUE; i++)
            sum += i;
        return sum;
    }
 private static long sum() {
        //如果使用Long 则需要自动拆箱操作
        Long sum = 0L;
        for (long i = 0L; i <= Integer.MAX_VALUE; i++)
            sum += i;
        return sum;
    }

以上代码 只有sum 的类型不一致,但两者的性能 差距很大,第一个在本机的运行时间 是 0.8s 第二个 是 10s 左右, 能清楚为什么吗? 欢迎大家回答吆

转载于:https://my.oschina.net/u/3247419/blog/3058400

猜你喜欢

转载自blog.csdn.net/weixin_34082177/article/details/92380009