Articles

Calculate method execution time in nano seconds, milli seconds and seconds in java

Calculate method execution time in nano seconds, milli seconds and seconds in java

In this tutorial, we will learn how to calculate execution time for java method in:

  • nano seconds,
  • milli seconds and
  • seconds

Java Program to calculate execution time in nano seconds

Given below is a simple java program that calculates execution time of a method in nano seconds.

package com.tutorialsinhand.practicals;

public class MethodExecutionTime {

	public static void main(String[] args) {
		
		long startTimeCaptured = System.nanoTime();
		printNumbers();
		long endTimeCaptured = System.nanoTime();
		
		//calculate execution time in nano seconds
		long executionTime = endTimeCaptured - startTimeCaptured;
		System.out.println("Java method execution time: "+ executionTime +" nanoseconds");

	}
	
	//method whose execution time is calculated
	public static void printNumbers(){
		for(int i = 0; i<50; i++){
			int result = i*i*i*i;
			System.out.println(result);
		}
	}

}

OUTPUT

0
1
16
::::::::::::::
not printing complete output for better comprehensibility
See complete output in eclipse
:::::::::::::
4879681
5308416
5764801
Java method execution time: 7120417 nano seconds
 
We can convert above result in nano seconds to milli seconds as shown below. To use below conversion, you need to import java.util.concurrent.TimeUnit
 
Note: please replace the main method in above program with below code snippet.
public static void main(String[] args) {
		
		long startTimeCaptured = System.nanoTime();
		printNumbers();
		long endTimeCaptured = System.nanoTime();
		
		//calculate execution time in nano seconds
		long executionTime = endTimeCaptured - startTimeCaptured;
		System.out.println("Java method execution time: "+ executionTime +" nano seconds");
		
		long convertNano2Milli = TimeUnit.NANOSECONDS.toMillis(executionTime);
		System.out.println("Coverted nano to milli seconds as: "+ convertNano2Milli);

}

Execute again and you will get output as:

...........................................

ommitted rest of the output

...........................................

Java method execution time: 6900326 nano seconds
Coverted nano to milli seconds as: 6

Java program to calculate execution time in milli seconds

Instead of converting the nano seconds to milliseconds, we can directly obtain the execution time of java method in milli seconds as shown below:

package com.tutorialsinhand.practicals;

public class MethodExecutionTimeInms {

	public static void main(String[] args) {
		
		long startTimeCaptured = System.currentTimeMillis();
		printNumbers();
		long endTimeCaptured = System.currentTimeMillis();
		
		//calculate execution time in milli seconds
		long executionTime = endTimeCaptured - startTimeCaptured;
		System.out.println("Java method execution time: "+ executionTime +" milli seconds");

	}
	
	//method whose execution time is calculated
	public static void printNumbers(){
		for(int i = 0; i<50; i++){
			int result = i*i*i*i;
			System.out.println(result);
		}
	}

}

OUTPUT

::::::::::::::

not printing out for better comprehensibility

:::::::::::::

5308416
5764801
Java method execution time: 7 milli seconds

 

To convert the time obtained in milli seconds to seconds just divide it by 1000 (1000ms = 1 second).

long timeInSeconds = executionTime/1000;
System.out.println("Java method execution time: "+ timeInSeconds +" seconds");

Just divide the executionTime value obtained in milli seconds by 1000 to get time in seconds

Hope you have got the basics, right?

Now you can just play around and try yourself to convert the same to minutes or hours.


Java Interview Questions
Java Error and Solution

Would you like to see your article here on tutorialsinhand. Join Write4Us program by tutorialsinhand.com

About the Author
Rohanjit Kumar
Technology geek, loves to write and share knowledge with the world. Having 9+ years of IT experience. B.Tech in Computer Science & Engineering
Page Views :    Published Date : Aug 15,2020  
Please Share this page

Related Articles

Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!