Java Programming

loop 안과 밖의 try/catch에 따른 performance

tomato13 2009. 6. 8. 20:08

IBM RSAEE(Rational Software Analyzer Enterprise Edition)라는 도구는 JAVA 코드를 입력받아 Static analysis를 수행한다. 


아래는 대상 샘플 코드이다.


static void test4() {

int l_cnt = 0;


System.out.println(System.currentTimeMillis());

for (; l_cnt < 3000; l_cnt++) {

try {

Thread.sleep(1);

} catch (Exception e) {

e.printStackTrace();

}

}

System.out.println(System.currentTimeMillis());

}


static void test5() {

int l_cnt = 0;


System.out.println(System.currentTimeMillis());

try {

for (; l_cnt < 3000; l_cnt++) {

Thread.sleep(1);

}

} catch (Exception e) {

e.printStackTrace();

}

System.out.println(System.currentTimeMillis());

}


test4()결과=>

1244458981930

1244458987790

(5860 msec)


test5()결과=>

1244458987790

1244458993666

(5876 msec)


도구에서는 test4()와 같은 방식을 피할 것을 권고하지만 실제 수행시간은 더 빠르거나 별다른 차이가 없는 듯 하였다.ㅡ.ㅡ;;


다음은 RASEE 에서 설명이다.

Try/catch blocks are necessary to insure proper exception handling, but creating such blocks has performance implications. Since loops contain intensive repetitive computations, it is not recommended to put try/catch blocks inside loops.