8월, 2006의 게시물 표시

[Java] Calendar is too slow

java.util.Calendar 객체를 생성하는 데는 상당한 비용이 소요된다. 즉, Calendar.getInstance() 라는 메소드를 호출하여 Calendar 객체를 가져오는 데 걸리는 시간이 상당하다. // 현재 시간을 가져오는 보통의 방법 private static Calendar getCalendar1() { return Calendar.getInstance(); } // 현재 시간은 아니지만 Calendar 객체를 clone을 통해 가져오는 방법 private static Calendar getCalendar2() { return (Calendar) cal.clone(); } // Calendar 객체를 clone을 통해 가져와서 현재 시간으로 만드는 방법 private static Calendar getCalendar3() { Calendar c = (Calendar) cal.clone(); c.setTimeInMillis(System.currentTimeMillis()); return c; } // 가장 범용적인 GregorianCalendar 객체를 직접 생성하는 방법 private static Calendar getCalendar4() { Calendar c = new GregorianCalendar(); return c; } 위의 메소드를 약 10만회씩 실행한 결과는 다음과 같다. // JDK 1.4 it took 391 (ms) it took 187 (ms) it took 375 (ms) it took 328 (ms) // JDK 1.5 it took 469 (ms) it took 266 (ms) it took 421 (ms) it took