
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
How to measure execution time for a Java method?
In general, the elapsed time is the time from the starting point to ending point of an event. Following are various ways to find elapsed time in Java −
- The currentTimeMillis() method returns the current time in milliseconds. To find the elapsed time for a method you can get the difference between time values before and after the execution of the desired method.
- The nanoTime() method returns the current time in nano seconds. To find the elapsed time for a method you can get the difference between time values before and after the execution of the desired method.
- The now() method of the Instant class returns the current time and the Duration.between() methods returns the difference between the given two time values to get the elapsed time retrieve the time values before and after the execution of the desired method and retrieve the duration using the Duration.between() method.
- The Apache commons library provides a class known as Stopwatch to it provides the start() stop() and getTime() methods to find the time taken for the execution of a method.
Example
Following example demonstrates how to find the execution time of a method using the above mentioned methods −
import java.time.Duration; import java.time.Instant; import org.apache.commons.lang3.time.StopWatch; public class Example { public void test(){ int num = 0; for(int i=0; i<=50; i++){ num =num+i; System.out.print(num+", "); } } public static void main(String args[]){ Example obj = new Example(); long start1 = System.nanoTime(); obj.test(); long end1 = System.nanoTime(); System.out.println("Elapsed Time in nano seconds: "+ (end1-start1)); long start2 = System.currentTimeMillis(); obj.test(); long end2 = System.currentTimeMillis(); System.out.println("Elapsed Time in milli seconds: "+ (end2-start2)); Instant inst1 = Instant.now(); obj.test(); Instant inst2 = Instant.now(); System.out.println("Elapsed Time: "+ Duration.between(inst1, inst2).toString()); StopWatch stopWatch = new StopWatch(); stopWatch.start(); obj.test(); stopWatch.stop(); System.out.println("Elapsed Time in minutes: "+ stopWatch.getTime()); } }
Output
0, 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 66, 78, 91, 105, 120, 136, 153, 171, 190, 210, 231, 253, 276, 300, 325, 351, 378, 406, 435, 465, 496, 528, 561, 595, 630, 666, 703, 741, 780, 820, 861, 903, 946, 990, 1035, 1081, 1128, 1176, 1225, 1275, Elapsed Time in nano seconds: 1882300 0, 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 66, 78, 91, 105, 120, 136, 153, 171, 190, 210, 231, 253, 276, 300, 325, 351, 378, 406, 435, 465, 496, 528, 561, 595, 630, 666, 703, 741, 780, 820, 861, 903, 946, 990, 1035, 1081, 1128, 1176, 1225, 1275, Elapsed Time in milli seconds: 1 0, 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 66, 78, 91, 105, 120, 136, 153, 171, 190, 210, 231, 253, 276, 300, 325, 351, 378, 406, 435, 465, 496, 528, 561, 595, 630, 666, 703, 741, 780, 820, 861, 903, 946, 990, 1035, 1081, 1128, 1176, 1225, 1275, Elapsed Time: PT0.001S 0, 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 66, 78, 91, 105, 120, 136, 153, 171, 190, 210, 231, 253, 276, 300, 325, 351, 378, 406, 435, 465, 496, 528, 561, 595, 630, 666, 703, 741, 780, 820, 861, 903, 946, 990, 1035, 1081, 1128, 1176, 1225, 1275, Elapsed Time in minutes: 1
- Related Articles
- How to measure the execution time in Golang?
- How do I time a method execution in Java
- Measure execution time of small Python code snippets (timeit)
- Measure execution time with high precision in C/C++
- How to calculate elapsed/execution time in Java?
- How to measure elapsed time in Java?
- Calculate the execution time of a method in C#
- How to measure the time taken by a function in Java?
- How to measure elapsed time in nanoseconds with Java?
- How to find PHP execution time?
- Java Program to Calculate the Execution Time of Methods
- How to schedule tasks in Java to run for repeated fixed-delay execution, beginning at the specified time
- How to schedule tasks in Java to run for repeated fixed-rate execution, beginning at the specified time
- Schedule a task for execution in Java
- How to measure actual MySQL query time?

Advertisements