Java – Measure elapsed time

Learn to calculate execution time or measure elapsed time of a program or some java statements using System.nanoTime() or System.currentTimeMillis() methods.

1. Measure elapsed time in Java 8

If we’re using Java 8 – we can try the new java.time.Instant and java.time.Duration classes. Below Java 8, proceed to next method down in the article.

To get the elapsed execution time in different time units, use methods such as toDays(), toHours(), toMillis(), toMinutes(), toNanos() and getSeconds().

import java.text.ParseException;
import java.util.concurrent.TimeUnit;

public class Main 
{
  public static void main(String[] args) throws ParseException 
  {
    Instant start = Instant.now();

    //Measure execution time for this method
    methodToTime();

    Instant finish = Instant.now();

    long timeElapsed = Duration.between(start, finish).toMillis();  //in millis
  }

  private static void methodToTime() {
    try {
      TimeUnit.SECONDS.sleep(3);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
  }
}

Program output.

3000076434    
3000

2. System.nanoTime() – Recommended method to measure elapsed time

This is the most recommended solution to measure elapsed time in Java. It provides nanoseconds level precision of elapsed time between two measurements. It is most preferred approach to calculate thread execution time in java.

import java.text.ParseException;
import java.util.concurrent.TimeUnit;

public class Main 
{
  public static void main(String[] args) throws ParseException 
  {
    long startTime = System.nanoTime();

    methodToTime();   //Measure execution time for this method

    long endTime = System.nanoTime();

    long durationInNano = (endTime - startTime);  //Total execution time in nano seconds

    //Same duration in millis
    
    long durationInMillis = TimeUnit.NANOSECONDS.toMillis(durationInNano);  //Total execution time in nano seconds
    
    System.out.println(durationInNano);
    System.out.println(durationInMillis);
  }

  private static void methodToTime() {
    try {
      TimeUnit.SECONDS.sleep(3);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
  }
}

Program output.

3000076434    //More precise
3000

3. System.currentTimeMillis() – milliseconds level precision

If you are not too concerned about nano level precision, or unfortunately still struck before Java 5 version – You shall be using System.currentTimeMillis() method.

import java.text.ParseException;
import java.util.concurrent.TimeUnit;

public class Main 
{
  public static void main(String[] args) throws ParseException 
  {
    long startTime = System.currentTimeMillis();

    methodToTime();   //Measure execution time for this method

    long endTime = System.currentTimeMillis();

    long duration = (endTime - startTime);  //Total execution time in milli seconds
    
    System.out.println(duration);
  }

  private static void methodToTime() {
    try {
      TimeUnit.SECONDS.sleep(3);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
  }
} 

Program output.

3001

We can convert above time in millis to other time units such as hours, minutes and seconds to measure execution time in corresponding time units.

Drop me your questions in comments section.

Happy Learning !!

Further reading:

System.currentTimeMillis vs System.nanoTime
SO Thread
Duration Java Docs

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

2 thoughts on “Java – Measure elapsed time”

  1. hello I am Faisal Seraj. I am a study and i am doing analysis of time complexity of three algorithms Selection , Insertion and Bubble sorting. So i need some check to see how many time each algorithm requires.

    Reply

Leave a Comment

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.