Java中new Integer() 和Integer.valueOf()二者效率的比较
最近在公司参与一个项目,项目挺大,耗时一年左右,具体业务就不说了。。。
之后在项目开发将近结束时,公司用Coverity工具对整体代码进行了检视,结果发现了N多问题,好多都是自己不注意的细节,在感叹此工具的强大的同时,问了下项目经理这个工具的价格,告知,30万$ !!! 纳尼
神马!尼玛
代码检视这块儿和findbug也差不多啊, 这也忒狠了点吧。。。
不扯淡了,步入正题。
在检视过程中,提到如下一个问题,就是在我们代码中用new Integer(a) 的地方,好多都提示说Ineffective way, use Integer.valueOf(int) intead. 一时感觉好奇,翻开源码查看,没有查出啥究竟,然后后来利用debug模式进入源码调试才发现,原来里面的实现也大有天地。。。
首先查看valueof(int) 方法的实现:
/** * Returns a <tt>Integer</tt> instance representing the specified * <tt>int</tt> value. * If a new <tt>Integer</tt> instance is not required, this method * should generally be used in preference to the constructor * {@link #Integer(int)}, as this method is likely to yield * significantly better space and time performance by caching * frequently requested values. * * @param i an <code>int</code> value. * @return a <tt>Integer</tt> instance representing <tt>i</tt>. * @since 1.5 */ public static Integer valueOf(int i) { if(i >= -128 && i <= IntegerCache.high) return IntegerCache.cache[i + 128]; else return new Integer(i); } 注释里面说的很清楚,“如果一个Integer的实例不是必须的,那么此方法应该优先于构造器来使用。。。”
为什么呢?继续深入探究,进入IntegerCache类,这个是Integer类的一个内部私有类。
private static class IntegerCache { static final int high; static final Integer cache[]; static { final int low = -128; // high value may be configured by property int h = 127; if (integerCacheHighPropValue != null) { // Use Long.decode here to avoid invoking methods that // require Integer's autoboxing cache to be initialized int i = Long.decode(integerCacheHighPropValue).intValue(); i = Math.max(i, 127); // Maximum array size is Integer.MAX_VALUE h = Math.min(i, Integer.MAX_VALUE - -low); } high = h; cache = new Integer[(high - low) + 1]; int j = low; for(int k = 0; k < cache.length; k++) cache[k] = new Integer(j++); } private IntegerCache() {} }至此,谜底基本解开,原来IntegerCache相当于做了一个缓存,在第一次被调用时,首先初始化生成了从-128到127共256个对象的数组,在以后凡是在这个范围内的int值都可以直接从此缓存中取,而不在再次生成Integer对象,大大提高了对象的利用率。
然后我做了一个测试类,来测试二者真正的效率区别:
public static void testEfficiency(){int count = 10000000;long t1 = System.currentTimeMillis();for(int i = 0; i < count; i ++){int a = new Integer(i%128 *(i%2 == 0?-1:1));}long t2 = System.currentTimeMillis();for(int i = 0; i < count; i ++){int b = Integer.valueOf(i%128 *(i%2 == 0?-1:1));}long t3 = System.currentTimeMillis();System.out.println("Time of new Integer() method:"+(t2-t1)+"ms");System.out.println("Time of Integer.ValueOf:"+(t3-t2)+"ms");}输出结果:
Time of new Integer() method:125msTime of Integer.ValueOf:94ms
二者似乎区别多大,不过这说明JDK虚拟机的效率比较高,
然后将这两种方式单独封装到两个方法中,利用两个程序进行调试,然后再看下javaw.exe所占的内存,区别就出来了。。。大家可以自己试一下,为了查看内存,可以将程序sleep 20s来查看, 我本机的测试结果:
new Integer方式:13 516K
Integer.valueOf方式:8 852K.
相差4664K.
实验基本到此结束,但是学习却只是一个开始,在实际项目中也可以参考此种实现方式,懒加载和缓存的思想。
已经纠正过来了,的确是-128 到127, 我写成了-127到128...
测试代码如下:
public static void main(String[] args) throws IOException {Integer a1 = 128;Integer b1 = 128;System.out.println(a1 == b1);Integer a2 = -128;Integer b2 = -128;System.out.println(a2 == b2);Integer a3 = 127;Integer b3 = 127;System.out.println(a3 == b3);}测试结果:
共同进步嘛! 4 楼 dy110936 2012-04-17 学习了,而且还发现如果是Integer = 100这种的话,自动装箱调用的也是valueOf 5 楼 feikiss 2012-04-21 dy110936 写道学习了,而且还发现如果是Integer = 100这种的话,自动装箱调用的也是valueOf
Integer i = n, n 为任意数,都会调用valueOf函数的。只不过当n为-128到127的时候,对象是从cache中直接获取的,并未生成。