How to calculate elapsed/execution time in Java?


In general, the elapsed time or, execution is the time from the starting point to ending point of an event. Following are various ways to find elapsed time in Java −

  • The currentTimeMillis() method returns the current time in milliseconds. To find the elapsed time for a method you can get the difference between time values before and after the execution of the desired method.
  • The nanoTime() method returns the current time in nano seconds. To find the elapsed time for a method you can get the difference between time values before and after the execution of the desired method.
  • The now() method of the Instant class returns the current time and the Duration.between() methods returns the difference between the given two time values you can find the time taken for the execution of a method using these.
  • The Apache commons library provides a class known as Stopwatch to it provides the start() stop() and getTime() methods you can find the time taken for the execution of a method using these.
  • The getTime() method of the java.util.Date class returns the current time. To find the elapsed time for a method you can get the difference between time values before and after the execution of the desired method.
  • You can also get the time in the current instance using the getInstance().getTime().getTime() after finding the elapsed time by getting the difference.

Example:Using the currentTimeMillis() method

Live Demo

public class Example {
   public void test(){
      int num = 0;
      for(int i=0; i<=50; i++){  
         num =num+i;
         System.out.print(num+", ");
      }  
   }
   public static void main(String args[]){  
      //Start time
      long begin = System.currentTimeMillis();
      //Starting the watch
      new Example().test();
      //End time
      long end = System.currentTimeMillis();      
     
      long time = end-begin;
      System.out.println();
      System.out.println("Elapsed Time: "+time +" milli seconds");
   }
}

Output

0, 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 66, 78, 91, 105, 120, 136, 153, 171, 190, 210, 231, 253, 276, 300, 325, 351, 378, 406, 435, 465, 496, 528, 561, 595, 630, 666, 703, 741, 780, 820, 861, 903, 946, 990, 1035, 1081, 1128, 1176, 1225, 1275,
Elapsed Time: 4 milli seconds

Example:Using the nanoTime() method

Live Demo

public class Example {
   public void test(){
      int num = 0;
      for(int i=0; i<=50; i++){  
         num =num+i;
         System.out.print(num+", ");
      }  
   }
   public static void main(String args[]){  
      //Start time
      long begin = System.nanoTime();
      //Starting the watch
      new Example().test();
      //End time
      long end = System.nanoTime();      
     
      long time = end-begin;
      System.out.println();
      System.out.println("Elapsed Time: "+time);
   }
}

Output

0, 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 66, 78, 91, 105, 120, 136, 153, 171, 190, 210, 231, 253, 276, 300, 325, 351, 378, 406, 435, 465, 496, 528, 561, 595, 630, 666, 703, 741, 780, 820, 861, 903, 946, 990, 1035, 1081, 1128, 1176, 1225, 1275,
Elapsed Time: 1530200

Example:Using the Instant class

Live Demo

import java.time.Duration;
import java.time.Instant;
public class Example {
   public void test(){
      int num = 0;
      for(int i=0; i<=50; i++){  
         num =num+i;
         System.out.print(num+", ");
      }  
   }
   public static void main(String args[]) {  
      //Starting time
      Instant start = Instant.now();
      new Example().test();
      //End time
      Instant end = Instant.now();
      long time = Duration.between(start, end).toMillis();
      System.out.println();
      System.out.println(time+" Milli seconds");

   }
}

Output

0, 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 66, 78, 91, 105, 120, 136, 153, 171, 190, 210, 231, 253, 276, 300, 325, 351, 378, 406, 435, 465, 496, 528, 561, 595, 630, 666, 703, 741, 780, 820, 861, 903, 946, 990, 1035, 1081, 1128, 1176, 1225, 1275,
3 Milli seconds

Example:Using the StopWatch class

import org.apache.commons.lang3.time.StopWatch;
public class Example {
   public void test(){
      int num = 0;
      for(int i=0; i<=50; i++){  
         num =num+i;
         System.out.print(num+", ");
      }  
   }
   public static void main(String args[]) {  
      //Instantiating the StopWatch class
      StopWatch obj = new StopWatch();
      //Starting the watch
      obj.start();
      new Example().test();
      //Stopping the watch
      obj.stop();
      System.out.println();
      System.out.println("Elapsed Time: "+obj.getTime() +" milli seconds");
   }
}

Output

0, 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 66, 78, 91, 105, 120, 136, 153, 171, 190, 210, 231, 253, 276, 300, 325, 351, 378, 406, 435, 465, 496, 528, 561, 595, 630, 666, 703, 741, 780, 820, 861, 903, 946, 990, 1035, 1081, 1128, 1176, 1225, 1275,
Elapsed Time: 1 milli seconds

Example: Using the getTime() method

Live Demo

import java.util.Date;
public class Demo{
   public void test(){
    int num = 0;
      for(int i=0; i<=50; i++){  
         num =num+i;
         System.out.print(num+", ");
      }  
   }
public static void main(String[] args) {
      long startTime = new Date().getTime();
       new Demo().test();
      long endTime = new Date().getTime();
      long time = endTime - startTime;
      System.out.println("Execution time: " + time);
   }
}

Output

0, 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 66, 78, 91, 105, 120, 136, 153, 171, 190, 210, 231, 253, 276, 300, 325, 351, 378, 406, 435, 465, 496, 528, 561, 595, 630, 666, 703, 741, 780, 820, 861, 903, 946, 990, 1035, 1081, 1128, 1176, 1225, 1275, Execution time: 5

Example: using the Calendar class

Live Demo

import java.util.Calendar;
public class Demo{
   public void test(){
    int num = 0;
      for(int i=0; i<=50; i++){  
         num =num+i;
         System.out.print(num+", ");
      }  
   }
public static void main(String[] args) throws InterruptedException {
      long startTime = Calendar.getInstance().getTime().getTime();
       new Demo().test();
      long endTime = Calendar.getInstance().getTime().getTime();
      long time = endTime - startTime;
      System.out.println("Execution time: " + time);
   }
}

Output

0, 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 66, 78, 91, 105, 120, 136, 153, 171, 190, 210, 231, 253, 276, 300, 325, 351, 378, 406, 435, 465, 496, 528, 561, 595, 630, 666, 703, 741, 780, 820, 861, 903, 946, 990, 1035, 1081, 1128, 1176, 1225, 1275, Execution time: 20

Updated on: 06-Feb-2021

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements