11

I would like to be able to see what code in thousands of lines of source code is run most often and takes the longest. The purpose of this would be for optimization.

Being able to see what parts of the code are run most often is important for optimization, because those parts are where I should focus to speed up. At the same time of course, some code is run very often but takes virtually no time, so its also important to be able to see what code takes the longest.

I guess the best of both worlds would be a program that adds up the amount of time a piece of code takes, including ALL of the times it was run (therefor figuring out what slows down your code most overall). Is there a kind of tools for this?

1

1 Answer 1

15

What you are looking for is profiling:

In software engineering, profiling ("program profiling", "software profiling") is a form of dynamic program analysis that measures, for example, the space (memory) or time complexity of a program, the usage of particular instructions, or frequency and duration of function calls. The most common use of profiling information is to aid program optimization.

There are several such applications. Sometimes, these are built into the IDE itself. The netbeans ide is one such tool that has a profiler built in. enter image description here

Another such tool is visual vm.

enter image description here

Profilers can work in different ways - and one should be aware of the differences. One approach is every second to take a snapshot of the threads running and see what methods are currently being invoked for each thread. This uses a sampling of what is out there.

The other approach is to instrument the java bytecode to report back the start and stop time to another application.

Writing a profiler is not terribly difficult for one skilled java. IBM has an article on writing your own profiler (with source code!). This one is based on the Java Interactive Profiler.

There are a multitude of profilers out there - both free and commercial (how about a profiler hooked up to running JVM instances that can report back to you (email or some other notification) when particular methods exceed the expected runtime? or hooked up to a program that reports back when web requests took too long - have the historical profile for that request saved off for later inspection?). You just have to know what you are looking for to start finding them and go from there.

Once you've got the profiler, you're on to interpreting the profiling results.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.