Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i benchmark method execution time in java?

I have a program which i have myself written in java, but I want to test method execution times and get timings for specific methods. I was wondering if this is possible, by maybe somehow an eclipse plug-in? or maybe inserting some code?

I see, it is quite a small program, nothing more than 1500 lines, which would be better a dedicated tool or System.currentTimeMillis()?

like image 621
KP65 Avatar asked Mar 31 '10 13:03

KP65


People also ask

What is JMH in Java?

JMH is short for Java Microbenchmark Harness. JMH is a toolkit that helps you implement Java microbenchmarks correctly. JMH is developed by the same people who implement the Java virtual machine, so these guys know what they are doing.


1 Answers

Other than using a profiler, a simple way of getting what you want is the following:

public class SomeClass{    public void somePublicMethod()    {        long startTime = System.currentTimeMillis();        someMethodWhichYouWantToProfile();        long endTime = System.currentTimeMillis();        System.out.println("Total execution time: " + (endTime-startTime) + "ms");     }  } 
like image 84
Chris Knight Avatar answered Oct 12 '22 23:10

Chris Knight