EN
Java stopwatch - how to measure elapsed time in java?
6
points
1. Overview
In this post we will review different ways of how to measure elapsed time in java.
Best way to measure elapsed time in java is to use Guava's Stopwatch.
Simple code snipped with Guava's Stopwatch:
Stopwatch stopwatch = Stopwatch.createStarted();
TimeUnit.SECONDS.sleep(2);
String time = stopwatch.toString();
System.out.println(time); // 2.001 s
2. Guava Stopwatch
import com.google.common.base.Stopwatch;
import java.util.concurrent.TimeUnit;
public class Example1 {
public static void main(String[] args) throws InterruptedException {
Stopwatch stopwatch = Stopwatch.createStarted();
for (int i = 0; i < 5; i++) {
TimeUnit.SECONDS.sleep(1);
String time = stopwatch.toString();
System.out.println(time);
}
}
}
Output:
1.001 s
2.018 s
3.019 s
4.019 s
5.019 s
3. System.currentTimeMillis()
import java.util.concurrent.TimeUnit;
public class Example2 {
public static void main(String[] args) throws InterruptedException {
long startTime = System.currentTimeMillis();
// our operation or method call takes 3 sec
TimeUnit.SECONDS.sleep(3);
long endTime = System.currentTimeMillis();
long timeElapsed = endTime - startTime;
System.out.println("Execution time in milliseconds: " + timeElapsed);
}
}
Output:
Execution time in milliseconds: 3001
4. System.nanoTime()
import java.util.concurrent.TimeUnit;
public class Example3 {
public static void main(String[] args) throws InterruptedException {
long startNanoTime = System.nanoTime();
// our operation or method call takes 3 sec
TimeUnit.SECONDS.sleep(3);
long endNanoTime = System.nanoTime();
long nanoTimeElapsed = endNanoTime - startNanoTime;
System.out.println("Execution time in nanoseconds : " + nanoTimeElapsed);
long elapsedTimeInMillis = nanoTimeElapsed / 1000000;
System.out.println("Execution time in milliseconds: " + elapsedTimeInMillis);
}
}
Output:
Execution time in nanoseconds : 3000492800
Execution time in milliseconds: 3000
5. Instant.now()
import java.time.Instant;
import java.util.concurrent.TimeUnit;
public class Example4 {
public static void main(String[] args) throws InterruptedException {
long startTime = Instant.now().toEpochMilli();
// our operation or method execution takes 3 sec
TimeUnit.SECONDS.sleep(3);
long endTime = Instant.now().toEpochMilli();
long timeElapsed = endTime - startTime;
System.out.println("Execution time in milliseconds: " + timeElapsed);
}
}
Output:
Execution time in milliseconds: 3002
6. Instant.now() with Duration between
import java.time.Duration;
import java.time.Instant;
import java.util.concurrent.TimeUnit;
public class Example5 {
public static void main(String[] args) throws InterruptedException {
Instant startTime = Instant.now();
// our operation or method execution takes 3 sec
TimeUnit.SECONDS.sleep(3);
Instant endTime = Instant.now();
Duration duration = Duration.between(startTime, endTime);
System.out.println("Execution in sec : " + duration.getSeconds());
}
}
Output:
Diff in sec : 3
7. Apache commons lang StopWatch
import org.apache.commons.lang3.time.StopWatch;
import java.util.concurrent.TimeUnit;
public class Example6 {
public static void main(String[] args) throws InterruptedException {
StopWatch stopwatch = new StopWatch();
stopwatch.start();
for (int i = 0; i < 5; i++) {
TimeUnit.SECONDS.sleep(1);
System.out.println(stopwatch.toString());
}
}
}
Output:
00:00:01.000
00:00:02.010
00:00:03.011
00:00:04.012
00:00:05.012
References
- Guava Stopwatch - JavaDoc
- Apache commons lang StopWatch - JavaDoc
- Guava - maven dependency
- Apache commons lang - maven dependency
- System.currentTimeMillis() - JavaDoc
- System.nanoTime() - JavaDoc
- Instant - JavaDoc
- Duration - JavaDoc
Did I miss anything? If you have better way to do it, leave a comment.