How to Find the Runtime of a Java Program

How to Find the Runtime of a Java Program | In this section, we will discuss how to measure execution time in Java. Execution time or the CPU time of a program or a task is the time taken by a system to execute a particular task or a program.

To find the runtime of a java program we can take the help of one of the below two methods:-
1) System.currentTimeMillis()
Method Syntax:- public static long currentTimeMillis()
This method returns the current time in milliseconds.
2) System.nanoTime()
Method Syntax:- pubic static long nanoTime()
This method returns the current value of the running Java Virtual Machine’s high-resolution time source, in nanoseconds.

How To Calculate Runtime In Java

Step1:- In the main class create a method display() to display the print statement.
Step2:- In the main method create an object for the main method and use the System.nanoTime() method then call the display method and print the statements required.

public class Main {

   public void display() {
      System.out.println("Main.display()");
   }

   public static void main(String[] args) {
      Main obj = new Main();
      // get current time before calling the method
      long start = System.nanoTime();

      obj.display();

      // get current time after calling the method
      long end = System.nanoTime();

      long executionTime = end - start;
      System.out.println("The display() method execution"+
             " time is: " + executionTime + " nanoseconds");
   }
}

Output:-

Main.display()
The display() method execution time is: 1147157 nanoseconds

On the different computers, you may get a different result and on multiple runs, you may get different results. The execution time completely depends upon the system’s performance.

Find the Runtime of a Java Program

Let us see another program to find the runtime of a Java program by using both nanoTime() and currentTimeMillis().

public class Main {
   public static void main(String[] args) {
      long initialNano = System.nanoTime();
      long initialMilli = System.currentTimeMillis();

      someFunction();

      long nano = System.nanoTime();
      long milli = System.currentTimeMillis();

      System.out.println("The execution time in nano seconds: "
                         + (nano - initialNano));
      System.out.println("The execution time in milli seconds: "
                         + (milli - initialMilli));
   }

   public static void someFunction() {
      for (int i = 0; i < Integer.MAX_VALUE; i++) {
         for (int j = 0; j < Integer.MAX_VALUE; j++) {
            // perform some operations
         }
      }
   }
}

Output:-

The execution time in nano seconds: 5512114
The execution time in milli seconds: 5

The execution time in nano seconds: 5070952
The execution time in milli seconds: 5

Measure Execution time in Java for a Recursive Method

The recursion is a technique that calls the same method again and again. The explanation of the below code to find the runtime of a Java program is as follows:-

Step-1:- Create a main class called Main and create a function factorial() which is a recursive function that finds the factorial of a number.
Step-2:- Then in the main method create an object of the main class and use the System.nanotime() method, Then subtract the start time and end time of the program and the difference in nanoseconds.

public class Main {

   public int fact(int n) {
      if (n != 0)
         return n * fact(n - 1);
      else
         return 1;
   }

   public static void main(String[] args) {
      Main obj = new Main();
      long start = System.nanoTime();

      obj.fact(128);

      long end = System.nanoTime();
      long executionTime = (end - start);

      System.out.println("The execution time of " +
                         "recursive method is: " + 
                         executionTime + " nanoseconds");
   }
}

Output:-

The execution time of recursive method is: 21542 nanoseconds

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

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