This post will discuss how to measure elapsed time in Java using System.nanoTime(), System.currentTimeMillis(), Instant.now() and StopWatch provided by Guava and Apache Commons Lang.

1. Using System.nanoTime() method

We can use System.nanoTime() to measure elapsed time with nanosecond precision. It returns the current value of the running JVM’s high-resolution time source in nanoseconds.

The value returned by this method has no meaning on its own, but it can become meaningful when the difference between two such values is computed.

Download  Run Code

Output (may vary):

Execution time in nanoseconds : 5000372657
Execution time in milliseconds: 5000

2. Using System.currentTimeMillis() method

Java System class also provides the static method currentTimeMillis() that returns the difference between the current time and midnight, January 1, 1970, UTC, in milliseconds.

Ideally, currentTimeMillis() should be used to measure wall-clock time, and nanoTime() should measure the program’s elapsed time. If the elapsed time is measured with System.currentTimeMillis(), the results might not be accurate.

Download  Run Code

Output (may vary):

Execution time in milliseconds: 5001

3. Using Instant.now() method

Instant class can be used to record event time-stamps in the application. It has now() method that obtains the current instant from the system clock. We can convert this instant to the total number of milliseconds using toEpochMilli() method.

Please note that Instant.now() internally use System.currentTimeMillis() and might not be the best solution to measure elapsed time.

Download  Run Code

Output (may vary):

Execution time in milliseconds: 5001

 
We can also calculate the duration between two Instant using the Duration.between method that returns a Duration instance. Then we can get the total number of seconds in this duration using the getSeconds or get method.

Download  Run Code

Output (may vary):

Execution time in seconds: 5

4. Using Guava’s StopWatch Class

We can also measure elapsed time using Guava’s StopWatch class instead of System.nanoTime() as StopWatch provides more abstraction by not exposing the absolute value returned by nanoTime(), which serves no importance. We can only get the relative values from the StopWatch.elapsed() method.

Download Code

Output (may vary):

Execution time in milliseconds: 5001

5. Using Apache Commons Lang

We can also use StopWatch API provided by Apache Commons Lang to measure execution time in milliseconds. To start the watch, simply call the start() or createStarted() method and then call getTime() to get the time between the start and the moment this method is called.

We can also call stop() method before getTime(), in that case getTime() returns the amount of time between start and stop.

Download Code

Output (may vary):

Execution time in milliseconds: 5002

6. Using Date.getTime() method

We can also use the getTime() method provided by the Date class that returns the total number of milliseconds since January 1, 1970, GMT.

Download  Run Code

Output (may vary):

Execution time in milliseconds: 5001

7. Using Calendar API

The following code uses Java’s Calendar API, which is not recommended as it adds extra overhead for a simple task.

Download  Run Code

Output (may vary):

Execution time in milliseconds: 5013

That’s all about measuring elapsed time in Java.