How to calculate elapsed / execute time in Java

In Java, you can use the following ways to measure elapsed time in Java.

1. System.nanoTime()

This is the recommended solution to measure elapsed time in Java.

ExecutionTime1.java

package com.mkyong.time;

import java.util.concurrent.TimeUnit;

public class ExecutionTime1 {

    public static void main(String[] args) throws InterruptedException {

		//start
        long lStartTime = System.nanoTime();

		//task
        calculation();

		//end
        long lEndTime = System.nanoTime();

		//time elapsed
        long output = lEndTime - lStartTime;

        System.out.println("Elapsed time in milliseconds: " + output / 1000000);

    }

    private static void calculation() throws InterruptedException {

        //Sleep 2 seconds
        TimeUnit.SECONDS.sleep(2);

    }
}

Output may vary.


2004

2. System.currentTimeMillis()

ExecutionTime2.java

package com.mkyong.time;

import java.util.concurrent.TimeUnit;

public class ExecutionTime2 {

    public static void main(String[] args) throws InterruptedException {

        long lStartTime = System.currentTimeMillis();

        calculation();

        long lEndTime = System.currentTimeMillis();

        long output = lEndTime - lStartTime;

        System.out.println("Elapsed time in milliseconds: " + output);

    }

    private static void calculation() throws InterruptedException {

        //Sleep 2 seconds
        TimeUnit.SECONDS.sleep(2);

    }
}

Output may vary.


2006

3. Instant.now().toEpochMilli()

In Java 8, you can try the new java.time.Instant

ExecutionTime3.java

package com.mkyong.time;

import java.time.Instant;
import java.util.concurrent.TimeUnit;

public class ExecutionTime3 {

    public static void main(String[] args) throws InterruptedException {

        long lStartTime = Instant.now().toEpochMilli();

        calculation();

        long lEndTime = Instant.now().toEpochMilli();

        long output = lEndTime - lStartTime;

        System.out.println("Elapsed time in milliseconds: " + output);

    }

    private static void calculation() throws InterruptedException {

        //Sleep 2 seconds
        TimeUnit.SECONDS.sleep(2);

    }
}

Output may vary.


2006

4. Date().getTime()

ExecutionTime4.java

package com.mkyong.time;

import java.util.Date;
import java.util.concurrent.TimeUnit;

public class ExecutionTime4 {

    public static void main(String[] args) throws InterruptedException {

        long lStartTime = new Date().getTime();

        calculation();

        long lEndTime = new Date().getTime();

        long output = lEndTime - lStartTime;

        System.out.println("Elapsed time in milliseconds: " + output);

    }

    private static void calculation() throws InterruptedException {

        //Sleep 2 seconds
        TimeUnit.SECONDS.sleep(2);

    }
}

Output may vary.


2007

References

  1. Stackoverflow – Is System.nanoTime() completely useless?
  2. Stackoverflow – System.currentTimeMillis vs System.nanoTime
  3. System#nanoTime JavaDoc
  4. Instant#toEpochMilli JavaDoc

12 comments on “How to calculate elapsed / execute time in Java

  1. So that we verify that the function actually does calculate the time right (As seen we sleep the program for 2 sec and indeed the calculation shows 2006 ms ~ 2sec )

  2. Android docs for System.currentTimeMillis() shows as
    “This method shouldn’t be used for measuring timeouts or other elapsed time
    measurements, as changing the system time can affect the results. Use nanoTime() for
    that.”

  3. What is the time equivalent in Swiss if CET time is reading at 2:30pm ?

    How do we calculate other time difference from java?

Leave a Comment

Your email address will not be published. Required fields are marked *