Java

[java] 실행시간 측정하는 3가지 방법

까오기 2021. 7. 29. 09:29

실행시간 측정하는 3가지 방법 

StopWatch - spring

StopWatch stopWatch = new StopWatch();
stopWatch.start();
//...
//...
//...
stopWatch.stop();
System.out.println("소요시간:"+stopWatch.getTotalTimeMillis()+"ms");
System.out.println(stopWatch.prettyPrint());

* apache comm-lang3에서도 제공되지만 둘다 thread safe 하지 않음 

 

Instant

Instant stime = Instant.now();
//...
//...
//...
Instant etime = Instant.now();
System.out.println("소요시간:"+Duration.between(stime, etime).toMillis()+"ms");

System.currentTimeMillis()

long stime = System.currentTimeMillis();
//...
//...
//...
System.out.println("소요시간:"+(System.currentTimeMillis()-stime)+"ms");