String.valueOf和字符串直接相加的性能比較

啥也不說了,直接代碼,別人的,我試的,哈哈:

/**
	 * 測試StringValueOf
	 * 2015年3月20日   wenzibin1    新建
	 */
	public static void testStringValueOf() {
		long t = System.currentTimeMillis();
        for (int i = 0; i < 1000000; i++) {
            String s0 = String.valueOf(11);
        }
        System.out.println("耗時" + (System.currentTimeMillis() - t));
 
        t = System.currentTimeMillis();
        for (int i = 0; i < 1000000; i++) {
            String s = "" + 11;
        }
        System.out.println("耗時" + (System.currentTimeMillis() - t));
         
        String str = "";
        t = System.currentTimeMillis();
        for (int i = 0; i < 1000000; i++) {
            String s = str + 11;
        }
        System.out.println("耗時" + (System.currentTimeMillis() - t));
	}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章