Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know which part of my code takes more CPU

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!

like image 759
TiagoM Avatar asked Mar 23 '13 11:03

TiagoM


3 Answers

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.

like image 120
Stephen C Avatar answered Oct 15 '22 11:10

Stephen C


First start with an abstract analysis, only then go into detail and measure.

Abstract analysis

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.

Measurement

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.

like image 24
DaveFar Avatar answered Oct 15 '22 11:10

DaveFar


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"
like image 20
dantuch Avatar answered Oct 15 '22 11:10

dantuch