Java program to calculate the execution time of methods:

     Execution is the final part in coding. It loads and links the object code into memory. While executing a code, it is important to measure the execution time. There are many methods to measure the execution time

        1.  Using nanoTime()

        2.   Using currentTimeMillis()

        3.   Using Instant and Duration

1.Using nanoTime():

              Here, we use nanoTime() built in method. This program gets two inputs as starting time and ending time using System.nanoTime(). Find the difference and store it in a variable. Print the variable.

public class nano {

    public static void main(String[] args) {

        // Reads the system time. let it be the starting time.

        long sTime = System.nanoTime();

        SimpleMeasure();

        // Gets the ending time

        long eTime = System.nanoTime();

        // find the execution time by finding different between staring and ending time

        long duration = (eTime - sTime); // in nanoseconds

        System.out.println("Execution time is: " + duration + " nanoseconds");

    }

       public static void SimpleMeasure(){

        // Example method to measure

        for (int i = 0; i < 1000000; i++) {

            // sample measure

        }

    }

}

Compile and run the program. You will get the below output.

2.Using System.currentTimeMillis():

              This method uses built in function currentTimeMillis(). This program reads two time for measuring the execution time.

Here, is the program.

public class Measure {

    public static void main(String[] args) {

        // Gets the starting time

        long sTime = System.currentTimeMillis();

        // Simple method to measure

        SimpleMeasure();

        // Gets the end time

        long eTime = System.currentTimeMillis();

        // find the execution time

        long duration = (eTime - sTime); // in milliseconds

        System.out.println("Execution time is: " + duration + " milliseconds");

    }

    public static void SimpleMeasure() {

        // simple method to measure

        for (int i = 0; i < 1000000; i++) {

            // simple process

        }

    }

}

When executing this program, you will get the output.

Ø  Execution time is: 453534 milliseconds

3. Using Instant and Duration:

              If you are using java8 and above, this method is used. This program uses two packages java.time.Duration, java.time.Instant.

Ø  Read the starting time and ending time using Instant class ‘s now() function.

Ø  Now, create an object for Duration class. Find the difference by using between() method.

Ø  Print the value using System.out.println().

import java.time.Duration;

import java.time.Instant;

public class Exec {

    public static void main(String[] args) {

        // Reads the starting time

        Instant st = Instant.now();

        SimpleMeasure();

        // Reads the ending time

        Instant end = Instant.now();

        // let find the execution time using duration

        Duration timeElapsed = Duration.between(st, end);

        System.out.println("Execution time is: " + timeElapsed.toMillis() + " milliseconds");

    }

    public static void SimpleMeasure() {

        for (int i = 0; i < 1000000; i++) {

            // some work

        }

    }

}

The output is

Ø  Execution time is: 353477 milliseconds

These are the ways to get the execution time.

No comments:

Post a Comment