How to measure time taken by a function in java ? Last Updated : 30 Apr, 2024 Comments Improve Suggest changes Like Article Like Report We can measure the time taken by a function in Java with the help of java.lang.System.nanoTime() and java.lang.System.currentTimeMills() methods. These methods return the current time in nanoseconds and milliseconds. We can call this method at the beginning and at the end of the function and by the difference we measure the time taken by the function. 1. Using System.nanoTime()The most common approach to calculating execution time is by utilizing the System.nanoTime() method, that provides a high-resolution timer. Here's how you can use it: Java // Java Program to demonstrate use // of System.nanoTime() method import java.io.*; // Driver class public class ExecutionTimeExample { // main function public static void main(String[] args) { // Start measuring execution time long startTime = System.nanoTime(); count_function(10000000); // Stop measuring execution time long endTime = System.nanoTime(); // Calculate the execution time in milliseconds long executionTime = (endTime - startTime) / 1000000; System.out.println("Counting to 10000000 takes " + executionTime + "ms"); } // A dummy function that runs a loop x times public static void count_function(long x) { System.out.println("Loop starts"); for (long i = 0; i < x; i++) ; System.out.println("Loop ends"); } } OutputLoop starts Loop ends Counting to 10000000 takes 12ms The System.nanoTime() method returns the current system time in nanoseconds. By capturing the start and end times and calculating the difference, we obtain the execution time in nanoseconds. To convert it to milliseconds, divide the difference by 1,000,000. 2. Using System.currentTimeMillis() An alternative approach to measure execution time is by using System.currentTimeMillis(). While this method provides lower precision than System.nanoTime(), it is still suitable for most use cases. Here's an example: Java // Java Program to demonstrate use // of System.currentTimeMillis() method import java.io.*; // Driver Class public class Time { // main function public static void main(String[] args) { // starting time long start = System.currentTimeMillis(); // start of function count_function(10000000); // end of function // ending time long end = System.currentTimeMillis(); System.out.println("Counting to 10000000 takes " + (end - start) + "ms"); } // A dummy function that runs a loop x times public static void count_function(long x) { System.out.println("Loop starts"); for (long i = 0; i < x; i++) ; System.out.println("Loop ends"); } } OutputLoop starts Loop ends Counting to 10000000 takes 18ms Similar ArticlesHow to measure time taken by a program in C?How to measure time taken by a program in Python? Comment More infoAdvertise with us Next Article How to measure time taken by a function in java ? S Surya Priy Follow Improve Article Tags : Java java-puzzle Practice Tags : Java Similar Reads java.time.Duration Class in Java Duration is the value-based Class present in the Java time library. It's used to get time-based amount of time. This class is immutable and thread-safe. This article describes all the methods present in this class and some basic examples using class methods. This class implements Serializable, Comp 4 min read Duration toNanos() method in Java with Examples The toNanos() method of Duration Class in java.time package is used get the value of this duration in number of nano-seconds. Syntax: public long toNanos() Parameters: This method do not accepts any parameter. Return Value: This method returns a long value which is the number of nano-seconds in this 1 min read Date setTime() method in Java with Examples The setTime() method of Java Date class sets a date object. It sets date object to represent time milliseconds after January 1, 1970 00:00:00 GMT. Syntax: public void setTime(long time) Parameters: The function accepts a single parameter time which specifies the number of milliseconds. Return Value: 2 min read Duration toSeconds() method in Java with Examples The toSeconds() method of Duration Class in java.time package is used get the value of this duration in number of seconds. Syntax: public long toSeconds() Parameters: This method do not accepts any parameter. Return Value: This method returns a long value which is the number of seconds in this durat 1 min read Duration toMinutes() method in Java with Examples The toMinutes() method of Duration Class in java.time package is used get the value of this duration in number of minutes. Syntax: public long toMinutes() Parameters: This method do not accepts any parameter. Return Value: This method returns a long value which is the number of minutes in this durat 1 min read Like