I would like to know which lines of my code are using most of the time of execution. I am doing a planner algorithm, and to solve a specific problem I made, the computer needs 5 minutes to find the solution.
I am using a lot of recursion methods, I would like to know where is the most time wasted, so I can look into that lines and try to fix or refactor the code to see if it helps.
I know that there are Cyclomatic Complexity Methods. But I don't know how to use it on Eclipse. I am using Helios. Tried to install metrics2 and it just pops up errors on my Eclipse.
EDITED: SMALLER NEW QUESTION:
http://s7.postimg.org/frd8yjql5/diag.png What does this means? Look at the Heap Size.. Always up and down.. does this affects CPU Speed? Thanks!
I would like to know which lines of my code are using most of the time of execution.
Run your code using a profiler. Eclipse has profiling support, but I'd recommend using the profiler supplied as part of the JDK - VisualVM.
(I would not start by analysing the "big O" complexity. Profile first, and that will tell you where to start looking. Then you might want to formally or informally look at the algorithmic complexity of the hotspots.)
I know that there are Cyclomatic Complexity Methods.
Cyclomatic complexity is not a predictor of performance. It is (supposedly) a measure of code maintainability, not of computational complexity. The same probably goes for the "metrics2" stuff, though I'm not familiar with it.
First start with an abstract analysis, only then go into detail and measure.
For the analysis, I would look at the complexity of your algorithm in the big O notation. That is the abstract analysis of runtime (whereas metrics like Cyclomatic Complexity look at code quality).
For algorithm analysis, I find Cormen's book "Introduction to algorithms" very good.
When you have an understanding of the complexity of your algorithm, you are able to check whether alternative algorithms would be better or where to do algorithmic improvements.
Avoid premature optimization: Only when you are sure you have a good algorithm, you should go into detail and measure.
To check whether your theoretical analysis was correct and to look at the technical implementation (mainly to search for hotspots and optimize those), you can use macro benchmarking tools and profilers like JVisualVM.
In most cases profiler is the best, but sometimes it's better to use a simple stopwatch: http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/base/Stopwatch.html
Basic usage:
Stopwatch stopwatch = new Stopwatch().start();
doSomething();
stopwatch.stop(); // optional
long millis = stopwatch.elapsed(MILLISECONDS);
log.info("that took: " + stopwatch); // formatted string like "12.3 ms"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With