标签:装箱拆箱
首先是包装类型
Long sum = Long.valueOf(0); long t1 = System.currentTimeMillis(); for (Long i = Long.valueOf(0); i < Integer.MAX_VALUE/2; i++) { sum += i; } t1 = System.currentTimeMillis() - t1; System.out.println("packaging took "+ t1 +" sum = "+ sum);
耗时:12759ms
装箱拆箱
Long sum = 0L; long t0 = System.currentTimeMillis(); for (long i = 0; i < Integer.MAX_VALUE/2; i++) { sum += i; } t0 = System.currentTimeMillis() - t0; System.out.println("Unboxing Boxing took "+ t0 +" sum = "+ sum);
耗时:6144ms
基本类型
long s = 0L; long t2 = System.currentTimeMillis(); for (long i = 0L; i < Integer.MAX_VALUE/2; i++) { s += i; } t2 = System.currentTimeMillis() - t2; System.out.println("prime took "+ t2 +" sum = "+ s);
耗时:581ms
速度之差立杆见影,基本类型的速度约是包装类型的21倍,装箱拆箱的10倍。
装箱拆箱那段代码是容易写错的地方,一个大小写之差。
优先使用基本类型的论点是有道理的。
包装类型、装箱拆箱、基本类型速度比较,布布扣,bubuko.com
标签:装箱拆箱
原文地址:http://blog.csdn.net/stephenluu/article/details/38725863