How to calculate execution time in java

How to calculate execution time in Java

In this article, we will see how to calculate execution time in java. There is a need of calculating execution time in java especially when there are multiple functions or methods. And we want to see how long a method takes time for execution.
For calculating an execution time in nanoseconds we can use an inbuilt java method of the System class. The function System.nanoTime() is used to measure time from the start to where we wrote that function.

Now let us understand with the help of some examples:

Calculate execution time in Java

How to find execution time in nanoseconds in java

Example :

import java.util.Scanner;
import java.util.NoSuchElementException;

public class MyClass { 
 public static void main(String args[]) {
		Scanner scanner = new Scanner(System.in);
		
		try {
            long start,end,result;
            int a,b,sum;
            Scanner s = new Scanner(System.in);
            
            System.out.println("Measuring execution time start");
            start=System.nanoTime();
            System.out.println("Enter first number");
            a = s.nextInt();
            
            System.out.println("Enter second number");
            b =  s.nextInt();
            sum= a+b;
            System.out.println("sum is :"+sum);
            end = System.nanoTime();
            System.out.println("Measuring execution time stop");
            
            result= end-start;
            System.out.println("Hence execution time in nanosecond for sum of two numbers is :"+result);
		} catch (NoSuchElementException e) {
		    System.out.println("Type something in the Stdin box above....");
		}
		
	}

	
}


Output :

Measuring execution time start
Enter first number
12
Enter second number
2
sum is :14
Measuring execution time stop
Hence execution time in nanosecond for sum of two numbers is :7757798672

In the above example, we find the time to find the sum of two numbers. First, we take a System.nanoTime() before taking a number that we want to calculate and store that time in the start variable i.e. start variable contains the time from starting of the program to where we place System.nanoTime(). Secondly, we take a System.nanoTime() after we get the sum and stored that time in the end variable i.e. end variable contains the time from starting of the program to where we place System.nanoTime(). Now we only want a time for the sum of two numbers therefore we subtract “end-start” for getting a time for that portion.

How to find method execution time in nanoseconds in Java

Example :

import java.util.Scanner;
import java.util.NoSuchElementException;

public class Main { 
    void multi(int n1,int n2) {
        int mul; 
        mul=n1*n2;
        System.out.println("Multiplication is :"+mul);
}
 public static void main(String args[]) {
		Scanner scanner = new Scanner(System.in);
		
		try {
	    Main m = new Main();
        Scanner s = new Scanner(System.in);
        System.out.println("Execution time starts\n");
        long start = System.nanoTime();
        
        System.out.println("Enter two numbers");
        int a = s.nextInt();
        int b = s.nextInt();
        //calling method
        m.multi(a,b);
        System.out.println("\nExecution time ends\n");
        
        long end = System.nanoTime();
        long result= end- start;
        System.out.println("Total execution time:"+result);
		} catch (NoSuchElementException e) {
		    System.out.println("Type something in the Stdin box above.... and then Execute");
		}
		
	}	
}


Output :

Execution time starts
Enter two numbers
12
2
Multiplication is :24
Execution time ends
Total execution time : 7474877126

Check out My Latest post on Developer Helps for some Interesting Insights


How to find execution time in milliseconds in Java

For calculating an execution time in milliseconds we have an inbuilt java method of the System class. The function is System.currentTimeMillis() which returns the current Timestamp in milliseconds.

Example :

import java.util.*;
public class Main
{

void multi() {
int mul;
int n1=7;
int n2=8;
mul=n1*n2;
System.out.println("Multiplication is :"+mul);
}

public static void main(String[] args) {
Main m = new Main();
System.out.println("Execution time starts\n");
long start = System.currentTimeMillis();
//calling method
m.multi();
System.out.println("\nExecution time ends");
long end = System.currentTimeMillis();
long result= end-start;
System.out.println("Total execution time in milliseconds:"+ result); 
}
}


Output :

Execution time starts
Multiplication is :56
Execution time ends
Total execution time in milliseconds:24

How to find execution time in seconds in Java

For calculating an execution time in seconds we have an inbuilt java method of the System class. The function is System.currentTimeMillis() which returns the current Timestamp in milliseconds.

Therefore, we must divide our milliseconds time to “1000” for calculating time in seconds.

Example :

import java.util.*;
public class Main
{

void multi() {
int mul;
int n1=7;
int n2=8;
mul=n1*n2;
System.out.println("Multiplication is :"+mul);
}

public static void main(String[] args) {
Main m = new Main();  
   
System.out.println("Execution time starts\n");
long start = System.currentTimeMillis();
//calling method
m.multi();
long end = System.currentTimeMillis();
System.out.println("\nExecution time ends");

long result= end-start;
System.out.println("Total execution time in milliseconds:"+ result); 
long secs= result/1000;
System.out.println("Total execution time in seconds:"+ secs);  
}


Output :

Execution time starts

Multiplication is :56
Execution time ends
Total execution time in milliseconds:65
Total execution time in seconds:0

Discover Our Exciting Courses and Quiz

Enroll now to enhance your skills and knowledge!

Java Online Quiz

Level up your coding skills with our interactive programming quiz!