Java Program to Calculate the Execution Time of Methods

Java Program to Calculate the Execution Time of Methods

What is  an Execution Time In Java ?

Execution time, also known as run time, refers to the amount of time it takes for a Java program or a specific method within a program to complete its execution. This can be useful for measuring the performance of a program or specific parts of a program, and can be used to identify bottlenecks or optimize performance.

 

Execution Time : 

In a Java program, execution time can be measured by getting the current system time before and after a specific operation or method is executed and then calculating the difference between the two times. This difference is the execution time for that operation or method in milliseconds.

Here is the pseudo code that demonstrates how to calculate the execution time of a method:

start_time = get_current_time()

# call the method you want to measure the execution time of here
method_to_measure()

end_time = get_current_time()

execution_time = end_time - start_time

print("Execution time: ", execution_time, " milliseconds")

Example 1:

Run

public class Main {
    public static void main(String[] args) {
        long startTime = System.nanoTime();
        // call the method you want to measure the execution time of here
        slowMethod();
        long endTime = System.nanoTime();
        long executionTime = endTime - startTime;
        System.out.println("Execution time: " + executionTime + " nanoseconds");
    }

    public static void slowMethod() {
        // some slow operation here
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

Output :

Execution time: 1000000000 nanoseconds

Note : Execution time may veries depending on the devices you are using.

Explanation : 

In this example, the main method calls System.nanoTime() to get the current system time before and after calling the slowMethod(). The difference between the two times is the execution time for the slowMethod() in nanoseconds.

The slowMethod() is just an example for a method that takes some time to execute. You can replace it with any method you want to measure the execution time of.

Example 2:

Run

import java.time.Instant;

public class Main {
    public static void main(String[] args) {
        Instant start = Instant.now();
        // call the method you want to measure the execution time of here
        slowMethod();
        Instant end = Instant.now();
        long executionTime = end.toEpochMilli() - start.toEpochMilli();
        System.out.println("Execution time: " + executionTime + " milliseconds");
    }

    public static void slowMethod() {
        // some slow operation here
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

Output :

Execution time: 1000 milliseconds

Explanation :

In this example, the main method calls Instant.now() to get the current system time before and after calling the slowMethod(). The difference between the two times is the execution time for the slowMethod() in milliseconds.

The slowMethod() is just an example for a method that takes some time to execute. You can replace it with any method you want to measure the execution time of.

You can see that the way to calculate the execution time is similar to the previous example, but this time we use Instant.now() to get the current time, and then use the toEpochMilli() method to get the time in milliseconds, and then subtract the start time from the end time to get the execution time in milliseconds.

Note : Both System.nanoTime() and Instant.now() are good ways to measure execution time, it depends on your requirements, you can choose the one that best suits your needs.

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription