2. System.currentTimeMillis() 소스 long startTime = System.currentTimeMillis();Thread.sleep(1000);long estimatedTime = System.currentTimeMillis() - startTime;System.out.println("took " + estimatedTime + " ms"); 결과 took 1000 ms
3. System.nanoTime() 소스 long time1 = System.nanoTime();Thread.sleep(1000);long time2 = System.nanoTime();long timeSpent = time2 - time1;System.out.println("took " + timeSpent + " ns"); 결과 took 1000232514 ns
4. Commons-lang의 StopWatch (2.3) import org.apache.commons.lang.time.StopWatch;..StopWatchstopWatch = new StopWatch();stopWatch.reset();stopWatch.start();Thread.sleep(2000);stopWatch.stop();System.out.println(stopWatch.toString()); stopWatch.reset();stopWatch.start();Thread.sleep(5000);stopWatch.stop();System.out.println(stopWatch.toString()); stopWatch.reset();stopWatch.start();Thread.sleep(3000);stopWatch.stop();System.out.println(stopWatch.toString()); 소스 0:00:02.0000:00:05.0000:00:03.000 결과
5. Spring core lib에 있는 util성 StopWatch (3.0) 소스 import org.springframework.util.StopWatch;...StopWatchstopWatch = new StopWatch("Stop Watch");stopWatch.start("initializing");Thread.sleep(2000);stopWatch.stop();System.out.println("took " + stopWatch.getLastTaskTimeMillis() + " ms"); stopWatch.start("processing");Thread.sleep(5000);stopWatch.stop();System.out.println("took " + stopWatch.getLastTaskTimeMillis() + " ms"); stopWatch.start("finalizing");Thread.sleep(3000);stopWatch.stop();System.out.println("took " + stopWatch.getLastTaskTimeMillis() + " ms"); System.out.println(stopWatch.toString());System.out.println();System.out.println(stopWatch.prettyPrint());
6. Spring core lib에 있는 util성 StopWatch (3.0) 결과 took 2000 mstook 5000 mstook 3001 msStopWatch 'Stop Watch': running time (millis) = 10001; [initializing] took 2000 = 20%; [processing] took 5000 = 50%; [finalizing] took 3001 = 30% StopWatch 'Stop Watch': running time (millis) = 10001-----------------------------------------ms % Task name-----------------------------------------02000 020% initializing05000 050% processing03001 030% finalizing
7. 남의 것 사용 그냥 인터넷에서 남들이 만든 StopWatch를 찾아서 copy&paste한다.http://www.devdaily.com/blog/post/java/stopwatch-class-that-can-be-used-for-timings-benchmarks