int 에서 String 으로 명시적인 형 변환 성능 Programmer's notes

백문이불여일견

public class IntStringTest {

    /**
     * @param args
     */
    public static void main(String[] args) {
        long start = System.currentTimeMillis();
        String str = null;
        int times = 10000000;
        for(int i=0;  i<times;i++) {
            str = Integer.toString(i);
        }
        long toString = System.currentTimeMillis();
        for(int i=0; i<times; i++) {
            str = ""+i;
        }
        long plus = System.currentTimeMillis();
        System.out.println(System.getProperty("java.vendor")+" "+System.getProperty("java.version"));
        System.out.println("toString = "+(toString-start)+" msec");
        System.out.println("plus = "+(plus-toString)+" msec");
    }

}
------------ JVM 별 결과 ------------------- 실행시스템 Sens X25 메모리 2G
BEA Systems, Inc. 1.4.2_08
toString = 1687 msec
plus = 4313 msec

Sun Microsystems Inc. 1.6.0_03
toString = 1563 msec
plus = 4594 msec

Sun Microsystems Inc. 1.4.2_05
toString = 2546 msec
plus = 5547 msec

Sun Microsystems Inc. 1.4.2_08
toString = 2625 msec
plus = 5453 msec

PMD 에서 Empty String 더하는 것을 지적하길리 한 번 돌려봤습니다.


트랙백

이 글과 관련된 글 쓰기 (트랙백 보내기)
TrackbackURL : http://sayjava.egloos.com/tb/4391170 [도움말]

덧글

  • 허진영 2008/06/02 07:01 # 답글

    확실히 명시적 형변환이 빠르군요. ㅎㅎㅎㅎㅎ
덧글 입력 영역