1

I am trying to find a clean way to find elapsed time. In other words, I want to find the time it takes for a given section of code to get executed.

I know the following two approaches:-

1>

long start = System.nanoTime();
// Code to measure
long elapsedTime = System.nanoTime() - start;

2>

long start = System.currentTimeMillis();
// Code to measure
long elapsedTime = System.currentTimeMillis() - start;

But these two approaches are dirty in the sense I have to litter the original code with my benchmarking code and later when benchmarking is over I will have to remember to remove the code.

I am looking for a cleaner approach which is supported by Eclipse/any other IDE in which I mark the code that I want to benchmark just like we specify breakpoints and Eclipse shows me the elapsed time whenever that portion of code is reached.

Is there any such method available in Eclipse/any other IDE?

2
  • Hey thanks! This answer seems to be the closest to what I am looking for. But as you have added a comment I don't know how to vote up/mark the answer as accepted. Commented May 5, 2012 at 16:50
  • I just deleted the comment and added the same answer as top level answer. Commented May 6, 2012 at 3:23

3 Answers 3

1

I recommend Perf4j perf4j.codehaus.org

Sign up to request clarification or add additional context in comments.

Comments

0

Set a field at the top of your class

private static final boolean DEBUG_THIS=true;

then when you have lines you want to use, put them in like this:

if (DEBUG_THIS){
    Log.v("Tag","It took " + System.currentTimeMillis() - start + " ms");
    // perhpas some other debug code
}

Then when you are tired or done debugging, change the one line of code at the top

private static final boolean DEBUG_THIS=false;

you can leave all your debug code otherwise in place, there if you ever need it again, and costing you nothing in the running version.

Comments

0

You can write a JUnit Test for the code. The JUnit plug-in for Eclipse will show you the time it took to run the test.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.