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?
我推荐Perf4j perf4j.codehaus.org
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.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.