How to Measure Elapsed Time (or Execution Time) in Java

In this post, we 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. System.nanoTime()

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. System.currentTimeMillis()

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 be used to measure the elapsed time of the program. 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. Instant.now()

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 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 Duration.between() method that returns a Duration instance. Then we can get the number of seconds in this duration using getSeconds() or get() method.

 

Download   Run Code

Output (may vary):

Execution time in seconds: 5

 

4. Guava’s StopWatch

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 StopWatch.elapsed() method.

 

Download   Run Code

Output (may vary):

Execution time in milliseconds: 5001

 

5. Apache Commons Lang StopWatch

We can also use StopWatch API provided by Apache Commons Lang to measure execution time in milliseconds. To start the watch, simply call 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

Output (may vary):

Execution time in milliseconds: 5002

 

6. Date.getTime()

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

 

Download   Run Code

Output (may vary):

Execution time in milliseconds: 5001

 

7. Calendar

Below 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

 
Thanks for reading.

Please use our online compiler to post code in comments.
Like us? Please spread the word and help us grow. Happy coding 🙂
 



Leave a Reply

avatar
  Subscribe  
newest oldest most voted
Notify of
bob
Guest

amazing!