Coders Packet

Java program to find total execution time

By Adinath Kishor Joshi

Hello learners, we will learn how to find the total execution time of a method using Java. Let us look at the algorithm and code :

Java language is widely used and is among the most popular languages used in creating various programs. For coding a program, saving time is very much important one must know the execution time of the program which can be calculated using this Java program.

ALGORITHM:

1. Create a method for which you have to find total execution time.
2. Create a Main method and object of the class.
3. Get the start time of the program.
4. Call the method.
5. Find the end time of the program.
6. Print the difference between the end time and the start time of the program to get the total execution time.

JAVA CODE:

Firstly create a method for finding execution time. In this example, we have taken a simple calculator example.

import java.util.Scanner;
public class Totaltime {

  public void perform() {
  
    Scanner sc = new Scanner(System.in);
    System.out.print("Enter two numbers: ");

    double a = sc.nextDouble();
    double b = sc.nextDouble();

    System.out.print("Enter an option (1-add,2-subtract,3-multiply,4-divide): ");
    char option = sc.next().charAt(0);

    double result;

    switch (option) {
      case '1':
        result = a+b;
        break;

      case '2':
        result = a-b;
        break;

      case '3':
        result = a*b;
        break;

      case '4':
        result = a/b;
        break;

      default:
        System.out.printf("Option is incorrect");
        return;
    }

    System.out.println("Result:" + result);
 
  }

Create a main method and create an object of the class Totaltime. Find the starting time of the program which can be obtained using nanoTime() method. This method returns the current time in nanoseconds.

After this step call the method and then after the method is executed find the ending time of the program using the nanoTime() method which gives us the current time after the program executes. The difference between the ending time and starting time gives us the total execution time.

 public static void main(String[] args) {

    Totaltime obj = new Totaltime();

    long begin = System.nanoTime();

    obj.perform();

    long terminate = System.nanoTime();

    System.out.println("The time taken to execute the program is " + (terminate - begin)+ " nanoseconds");
  }
}

Save, compile and run. The Execution time for addition, subtraction, multiplication, and division are obtained :

C:\java>java Totaltime
Enter two numbers: 5
4
Enter an option (1-add,2-subtract,3-multiply,4-divide): 1
Result:9.0
The time taken to execute the program is 4399358800 nanoseconds

C:\java>java Totaltime
Enter two numbers: 5
4
Enter an option (1-add,2-subtract,3-multiply,4-divide): 2
Result:1.0
The time taken to execute the program is 5372959400 nanoseconds

C:\java>java Totaltime
Enter two numbers: 5
4
Enter an option (1-add,2-subtract,3-multiply,4-divide): 3
Result:20.0
The time taken to execute the program is 2845506600 nanoseconds

C:\java>java Totaltime
Enter two numbers: 5
4
Enter an option (1-add,2-subtract,3-multiply,4-divide): 4
Result:1.25
The time taken to execute the program is 2374615000 nanoseconds

 

Download project

Reviews Report

Submitted by Adinath Kishor Joshi (adinathjoshi)

Download packets of source code on Coders Packet