How to calculate method execution time in Java

In this article, you will learn how to calculate method execution time in Java programming language. Most of the time you need to calculate method execution time or block execution time of your program to measure performance of your program.

Today we will discuss two different ways to calculate method execution time in Java. You can either use System.currentTimeinMillis() or System.nanoTime() to calculate the execution time of any method. It is better to use System.nanoTime() because it provides the execution time in nano seconds which is imporatnt when the time of execution is in range of few milliseconds.

Let us understand how to use these methods in Java program to calculate the execution time of any method

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.TreeSet;

public class TimeCalculatorExample {

	public static void main(String[] args) {
		List<Integer> countryName = new ArrayList<Integer>();
		for(int i =1; i<10000; i++){
			Random r = new Random();
			countryName.add(r.nextInt());
		}
		timeCalculatordemo(countryName);
	}
	
	public static void timeCalculatordemo(List countryName){
		long startTime = System.currentTimeMillis();
		TreeSet<Integer> tSet = new TreeSet<Integer>();
		tSet.addAll(countryName);
		long endTime = System.currentTimeMillis();

		long executionTime = endTime - startTime;

		System.out.println("Elapsed milliseconds: " + executionTime);
	}

}

Output

Elapsed milliseconds: 16

So the above method block of code as shown between startTime and endTime took 16 millisecond to execute when there were 10000 elements in ArrayList. Now let us consider the number of elements be 1000. The execution time will be significantly in range of few milliseconds. Hence, in such cases you should prefer System.nanoTime() to calculate the execution time of any method as shown.

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.TreeSet;

public class TimeCalculatorExample {

	public static void main(String[] args) {
		List<Integer> countryName = new ArrayList<Integer>();
		for(int i =1; i<1000; i++){
			Random r = new Random();
			countryName.add(r.nextInt());
		}
		timeCalculatordemo(countryName);
	}
	
	public static void timeCalculatordemo(List countryName){
		long startTime = System.nanoTime();
		TreeSet<Integer> tSet = new TreeSet<Integer>();
		tSet.addAll(countryName);
		long endTime = System.nanoTime();

		long executionTime = endTime - startTime;

		System.out.println("Elapsed nanoseconds: " + executionTime);
	}

}

Output

Elapsed nanoseconds: 2255918

Now try to calculate execution time using System.currentTimeMillis() for 1000 elements and see the difference of precision between System.currentTimeMillis() and System.nanoTime() methods.It is important to know that System.nanoTime() method is available in Java 5 and higher versions.

In this article we discussed  how to calculate method execution time in Java programming language. It is really important to understand the usage of these two methods because you can significantly improve your logic or implementation to take less time to execute the code block.

Article tagged as
Author
Author: Amit Gupta
Published On: 19/11/2015
Last revised On: 20/11/2015
View all articles by Amit Gupta

Share this post

Comments

Comments
comments powered by Disqus

Navigation

Social Media