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); }
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() {} }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
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);}