Java 8 自动装箱拆箱效率影响测试

版权声明:本文为博主原创文章, 不经同意就可以随便转载. https://blog.csdn.net/jam1105/article/details/85034621

Java 8 自动装箱拆箱效率影响测试


测试完毕之后的结论:
  1. 装箱发生 23212^{32-1} 次的时候影响时间大概为10秒
  2. 拆箱发生 23212^{32-1} 次的时候影响时间为10~20秒
因为 System.gc() 并不会立即执行垃圾回收所以不确定具体对效率的影响.

拆箱/装箱 影响效率的主要原因是会产生很多的小对象, 影响垃圾回收.
拆箱: 在栈上创建一个基本类型的数据.
装箱: 需要在堆上创建一个新的对象.

如果想要尽量避免自动装箱/拆箱 对效率有影响就需要在设置数据类型的时候认真思考下.

测试代码如下.

public void autoBoxingTest() {
        /**
         *
         * assignment : assignNormal: 2147483646 begin: 1550286447879 middle: 1550286450002 normal: 2123
         * assignment : assignBoxing: 2147483646 middle: 1550286450002 end: 1550286461670 boxing: 11668
         *
         */
        long assignNormal = 0L;
        Long assignBoxing = 0L;
        long begin = System.currentTimeMillis();

        for (long i = 0L; i < Integer.MAX_VALUE; i++) {
            assignNormal = i;
        }

        long middle = System.currentTimeMillis();
        for (long i = 0L; i < Integer.MAX_VALUE; i++) {
            // 装箱 long -> Long
            assignBoxing = i;
        }

        long end = System.currentTimeMillis();

        System.out.println("assignment : assignNormal: " + assignNormal + " begin: " + begin + " middle: " + middle + " normal: " + (middle - begin));
        System.out.println("assignment : assignBoxing: " + assignBoxing + " middle: " + middle + " end: " + end + " boxing: " + (end - middle));

        /**
         *
         * add : sumNormal: 2305843005992468481 begin: 1550286461670 middle: 1550286462474 normal: 804
         * add : sumBoxing: 2305843005992468481 middle: 1550286462474 end: 1550286474223 boxing: 11749
         *
         */
        long sumNormal = 0L;
        Long sumBoxing = 0L;
        begin = System.currentTimeMillis();

        for (long i = 0L; i < Integer.MAX_VALUE; i++) {
            sumNormal += i;
        }

        middle = System.currentTimeMillis();
        for (long i = 0L; i < Integer.MAX_VALUE; i++) {
            // 装箱 long -> Long
            sumBoxing += i;
        }

        end = System.currentTimeMillis();

        System.out.println("add : sumNormal: " + sumNormal + " begin: " + begin + " middle: " + middle + " normal: " + (middle - begin));
        System.out.println("add : sumBoxing: " + sumBoxing + " middle: " + middle + " end: " + end + " boxing: " + (end - middle));

    }

    public void autoBoxedTest() {
        /**
         * assignment  : normal :2147483646 begin: 1550286528618 middle: 1550286538433 normal spent: 9815 ms
         * assignment  : boxed : 2147483646 middle: 1550286538433 end: 1550286560456 boxed spent: 22023 ms
         */
        Long normal = 0L;
        long boxed = 0L;
        long begin = System.currentTimeMillis();

        for (Long i = 0L; i.compareTo(new Long(Integer.MAX_VALUE)) < 0; i++) {
            normal = i;
        }
        long middle = System.currentTimeMillis();
        for (Long i = 0L; i.compareTo(new Long(Integer.MAX_VALUE)) < 0; i++) {
            // 拆箱 Long -> long
            boxed = i;
        }
        long end = System.currentTimeMillis();

        System.out.println("assignment  : normal :" + normal + " begin: " + begin + " middle: " + middle + " normal spent: " + (middle - begin) + " ms");
        System.out.println("assignment  : boxed : " + boxed + " middle: " + middle + " end: " + end + " boxed spent: " + (end - middle) + " ms");

        /**
         * add  : normal :2305843005992468481begin: 1550286560457 middle: 1550286577430 normal spent: 16973 ms
         * add  : boxed : 2305843005992468481middle: 1550286577430 end: 1550286601522 boxed spent: 24092 ms
         */
        begin = System.currentTimeMillis();
        normal = 0L;
        boxed = 0L;

        for (Long i = 0L; i.compareTo(new Long(Integer.MAX_VALUE)) < 0; i++) {
            normal += i;
        }
        middle = System.currentTimeMillis();
        for (Long i = 0L; i.compareTo(new Long(Integer.MAX_VALUE)) < 0; i++) {
            // 拆箱 Long -> long
            boxed += i;
        }
        end = System.currentTimeMillis();

        System.out.println("add  : normal :" + normal + "begin: " + begin + " middle: " + middle + " normal spent: " + (middle - begin) + " ms");
        System.out.println("add  : boxed : " + boxed + "middle: " + middle + " end: " + end + " boxed spent: " + (end - middle) + " ms");
    }
展开阅读全文

没有更多推荐了,返回首页