Lonefish
Lonefish

Reputation: 677

How can I check which part of a method uses the most time?

I'm working on a project for optimizing an existing algorithm. I profiled the code with JIP and VisualVM and got a main bottleneck.

Now I want to see what takes the most time IN the method. What is an easy way to do that? Are there profilers that give me that information or do I need to start timing every step in the method and write it to a file?

EDIT : This should be while the code is running on inputsets

EDIT 2 : The method I'm talking about is called 100million+ times with a total of +/-500 seconds runtime. So every optimisation in the code will have a significant improvement.

Upvotes: 1

Views: 1751

Answers (3)

Mike Dunlavey
Mike Dunlavey

Reputation: 40679

Don't do it by measuring time.

Do it by grabbing snapshots in the debugger. That will show you down to the finest level what it's doing. Whatever it's doing, if you catch it doing it on two or more snapshots - consider it a bug. If you can avoid it, you'll save a ton of time.

How much time? You'll find out when you fix it, but the fewer snapshots it took to see it twice, the more you'll save.

If you're afraid it won't find "the problem", don't worry, it will, and it's probably something you didn't expect. More on all that ...

Upvotes: 0

user626607
user626607

Reputation:

If you are looking for tools that analyze code while it is executing, you will want to investigate dynamic analysis tools. JTest (for Java) is mentioned in the article but I am afraid I have not worked with it.

You can alternatively use a static code analysis tool like soot with some varied levels of success. The tools will identify areas of your code that take the longest to execute.

Others include: SonarJ, PMD, Kalistick

The way that I have used the tools like soot is to bind it in my build management tool. So for maven, it would be configured like so:

  <plugin>
    <groupId>net.ju-n.maven.plugins</groupId>
    <artifactId>soot-maven-plugin</artifactId>
    <version>0.1.1</version>
    <executions>
      <execution>
        <goals>
          <goal><!-- put the goal name here --></goal>
        </goals>
      </execution>
    </executions>
    <configuration>
        <!-- put your configurations here -->
    </configuration>
  </plugin>

I would then configure this to run in a maven profile (preferably on a build server) and publish the results.

The goal to execute is: mvn soot:soot

Note that the static analysis is performed on the code without actually executing the code.

Upvotes: 1

vector
vector

Reputation: 7576

JUnitBenchmarks could help. What about a 'brute force' type of check with time-stamps

between relevant sections of the method?

Upvotes: 0

Related Questions