How does one measure the performance of a method/period of time in an application in Java? Is there a class made specifically for this functionality?
- 
                    2Well how do you define "performance"? Now measure this definition. If you're talking about time to execute: `System.nanoTime()` is usually recommended over `System.currentTimeMillis()`, but that's as specific an answer as you can get. – Jeroen Vannevel Apr 11 '14 at 21:34
- 
                    Like in seconds, not asymptoptic analysis – George Newton Apr 11 '14 at 21:35
4 Answers
There are two ways:
- Testing at design time. This means, you will perform your tests when designing the application. You may be tempted to create some methods and naively measure them by using - System.nanoTime()(or, in worst case,- System.currentTimeMillis()), and this is a naive approach because you're missing lot of concepts for handling a micro benchmark. Still, if you want to manually benchmark your methods, at least follow proper rules for a micro benchmark. IMO you should not reinvent the wheel, instead use a benchmark framework like JUnitBenchmarks or Caliper where you can measure the time of your algorithms/methods using unit testing.
- Testing at run time. This means, you will measure the performance when the application is running, usually in a dedicated environment similar to production environment. For this scenario, use a profiler. There are lot of profilers for Java applications, from free license like VisualVM and Java Mission Control (this comes from JDK 7 u40) that are shipped in the JDK distribution to commercial products like Yourkit. 
 
    
    - 1
- 1
 
    
    - 85,076
- 16
- 154
- 332
In Java8, there is a new tool for this, have a look at http://openjdk.java.net/projects/code-tools/jmh/
JMH is a Java harness for building, running, and analysing nano/micro/milli/macro benchmarks written in Java and other languages targetting the JVM.
 
    
    - 6,412
- 2
- 29
- 38
You can use System.currentTimeMillis() or System.nanoTime() to get the time before and after the execution of the code you want to measure.
A more sofisticated approach would be to use JMeter or a similar app for generating statistics of many executions.
An even more sofisticated approach would be to use Perf4j.
 
    
    - 10,561
- 4
- 45
- 63
- 
                    1[`System.nanoTime()` is advised over `System.currentTimeMillis()`](http://stackoverflow.com/questions/351565/system-currenttimemillis-vs-system-nanotime). – Jeroen Vannevel Apr 11 '14 at 21:39
- 
                    *You can use System.currentTimeMillis() to get the time before and after the execution of the code* it would be better using [`System.nanoTime()`](http://docs.oracle.com/javase/7/docs/api/java/lang/System.html#nanoTime()) – Luiggi Mendoza Apr 11 '14 at 21:39
- 
                    AFAIK JMeter works for web based applications, doesn't work for desktop or console apps. – Luiggi Mendoza Apr 11 '14 at 21:40
- 
                    
- 
                    Also, this doesn't work well since it is a micro benchmark. Follow the [proposed rules for good microbenchmarking](http://stackoverflow.com/q/504103/1065197). – Luiggi Mendoza Apr 11 '14 at 21:46
- 
                    1
I'm using this http://metrics.codahale.com/ and it rocks. It comes packaged in DropWizard which is nice. Also pick up a profiler -> yourKit or JMC (free Java Mission Control).
The key is to be lightweight when profiling. You don't want to add too much bias to the equation.
 
    
    - 567
- 1
- 4
- 12
