Java拆箱性能损耗不容忽视

 

public class JavaBoxCost {

    public static void main(String[] args) {

        long start = System.currentTimeMillis();
        for (int i = 0; i < Integer.MAX_VALUE - 10; i++) {
            Integer va = i;
        }
        System.out.println("cost = " + (System.currentTimeMillis() - start));

        start = System.currentTimeMillis();
        for (Integer i = 0; i < Integer.MAX_VALUE - 10; i++) {
            int va = i;
        }
        System.out.println("cost = " + (System.currentTimeMillis() - start));

        /**
         * 最终输出:
         * cost = 5
         * cost = 4781
         */
    }
}

  

posted @ 2021-09-16 21:55  bf378  阅读(87)  评论(1编辑  收藏  举报