Java基本类型int与类类型Integer性能差别

package com.hknaruto;

import java.util.Random;

/**
 * Created by yeqiang on 4/24/18.
 */
public class IntegerPerformanceTest {
    public static void main(String[] args) {
        long start, end;
        int rand = new Random().nextInt();
        start = System.nanoTime();
        int c = 0;
        for (Integer i = 0; i < 100000000; i++) {
            c += rand + i;
        }
        end = System.nanoTime();
        System.out.println(c);
        System.out.println(end - start);

        start = System.nanoTime();
        c = 0;
        for (int i = 0; i < 100000000; i++) {
            c += rand + i;
        }
        end = System.nanoTime();
        System.out.println(c);
        System.out.println(end - start);
    }
}

输出

-1508346240
396963018
-1508346240
34686095

猜你喜欢

转载自blog.csdn.net/hknaruto/article/details/80063599