How do I get a method's execution time? Is there a Timer utility class for things like timing how long a task takes, etc?
Most of the searches on Google return results for timers that schedule threads and tasks, which is not what I want.
How do I get a method's execution time? Is there a Timer utility class for things like timing how long a task takes, etc?
Most of the searches on Google return results for timers that schedule threads and tasks, which is not what I want.
Just a small twist, if you don't use tooling and want to time methods with low execution time: execute it many times, each time doubling the number of times it is executed until you reach a second, or so. Thus, the time of the Call to System.nanoTime and so forth, nor the accuracy of System.nanoTime does affect the result much.
Of course, the caveats about using the wall clock apply: influences of JIT-compilation, multiple threads / processes etc. Thus, you need to first execute the method a lot of times first, such that the JIT compiler does its work, and then repeat this test multiple times and take the lowest execution time.
Also We can use StopWatch class of Apache commons for measuring the time.
Sample code
Gathered all possible ways together into one place.
Date
System.currentTimeMillis()
Human Readable Format
Guava: Google StopwatchJAR « An object of Stopwatch is to measures elapsed time in nanoseconds.
Apache Commons LangJAR « StopWatch provides a convenient API for timings.
JODA-TIME
Java date time API from Java 8 « A Duration object represents a period of time between two Instant objects.
Spring Framework provides StopWatch utility class to measure elapsed time in Java.
OutPut:
I have written a method to print the method execution time in a much readable form. For example, to calculate the factorial of 1 Million, it takes approximately 9 minutes. So the execution time get printed as:
The code is here:
You can use stopwatch class from spring core project:
Code:
Documentation for Stopwatch: Simple stop watch, allowing for timing of a number of tasks, exposing total running time and running time for each named task. Conceals use of System.currentTimeMillis(), improving the readability of application code and reducing the likelihood of calculation errors. Note that this object is not designed to be thread-safe and does not use synchronization. This class is normally used to verify performance during proof-of-concepts and in development, rather than as part of production applications.
System.currentTimeMillis();
IS NOT a good approach for measuring the performance of your algorithms. It measures the total time you experience as a user watching the computer screen. It includes also time consumed by everything else running on your computer in the background. This could make a huge difference in case you have a lot of programs running on your workstation.Proper approach is using
java.lang.management
package.From http://nadeausoftware.com/articles/2008/03/java_tip_how_get_cpu_and_user_time_benchmarking website:
getCpuTime()
method gives you sum of those: