Calculate the Execution Time of Method in Java

Exercise:

Write a Java Program to calculate the execution time of methods.

1.calculate the method execution time

Click Here to View the Solution!
public class ExTime {
    public void display() {
        System.out.println("Calculating this method’s execution time:");
    }
    public static void main(String[] args) {
      
        //create an object of the ExTime class
        ExTime obj = new ExTime ();
       
        // getting start time
        long start = System.nanoTime();
        
        // calling ethod
        obj.display();
       
        //getting end time
        long end = System.nanoTime();
       
        //execution time calculation
        long execution = end - start;

    System.out.println("Execution time: " + execution + " nanoseconds");
    }
}
Click Here to View the Output!
Calculating this method’s execution time:
 Execution time: 1186508 nanoseconds
Click Here to View the Explanation!
  • In this program, first a class called ExTime is created.
  • This class has a display() method which prints out a string on the screen.
  • Basically, the time taken to execute this method display() has been calculated in this program.
  • In the main method of this program, first an object called obj is created.
  • Next, the current time in nanoseconds has been saved in a long type variable called start. The current time is taken by a System class’s method called nanoTime() which gives the time in nano seconds.
  • Afterwards, the display() method is called.
  • Then again, the current time is stored in nanoseconds in a long type variable called end.
  • Difference between end and start variables’ values is stored in a long type variable called execution. This signifies the time of execution for display() method.
  • Lastly, this execution time is printed on the screen

2.calculate Recursive method’s execution time

Click Here to View the Solution!
public class ExTime {
    // recursive method declaration
    public int factorial( int n ) {
        if (n != 0)
            return n * factorial(n-1);
        else
            return 1;
    }
  
    // main method
    public static void main(String[] args) {
        
        // create object of Main class
        ExTime obj = new ExTime();

        // getting start time
        long start = System.nanoTime();

        // calling method
        obj.factorial(128);

        // getting end time
        long end = System.nanoTime();

        // calculating execution time
        long execution = (end - start);
        System.out.println("Execution time for this Recursive Method is");
        System.out.println(execution + " nanoseconds");
    }
}
Click Here to View the Output!
Execution time for this Recursive Method is
 20560 nanoseconds
Click Here to View the Explanation!
  • In this program, first a class called ExTime is created.
  • Then a recursive method called factorial is created which calculates a number’s factorial through recursive steps.
  • In the main method of this program, first an object called obj is created.
  • Next, the current time in nanoseconds has been saved in a long type variable called start. The current time is taken by a System class method called nanoTime() which gives the time in nano seconds.
  • Afterwards, the factorial() method is called with a number passed as its argument.
  • Then again, the current time is stored in nanoseconds in a long type variable called end.
  • Difference between end and start variable’s values is stored in a long type variable called execution. This signifies the time of execution for factorial() method.
  • Lastly, this execution time is printed on the screen.

%d bloggers like this: