How To Measure Elapsed Time in Java

Learn to calculate execution time or measure elapsed time of a program or some Java statements using various techniques pre and post Java 8 release.

1. Measuring Elapsed Time since 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 the next method down in the article.

To get the elapsed execution time in different time units, use the following method. It measures the duration between two Instants. And Instant represents the time elapsed since the epoch.

long timeElapsed = Duration.between(startInstant, finishInstant).toMillis();
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.

3000

2. System.nanoTime()

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 the 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()

If you are not too concerned about nano level precision, or unfortunately still stuck in legacy Java versions – 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.

3000

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

Happy Learning !!

Was this post helpful?

Join 8000+ Awesome Developers, Like YOU!

2 thoughts on “How To Measure Elapsed Time in Java”

  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

About HowToDoInJava

This blog provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions, and frequently asked interview questions.