views:

491

answers:

4

In Java VisualVM, is there any way to display total method time, rather than "self time"? (The latter is not particularly useful, since it doesn't tell you anything about how much time methods actually take to run.)

If not, is there any standalone free Java profiler that does calculate total method time?

A: 

you could use a

 long startTime = System.currentTimeMillis();

at the beggining

and

 long endTime = System.currentTimeMillis();

and finally to get the result

 long result = endTime - startTime; //Note, part might be backwards, I don't
                                    //Remember
Brendan
end - start... otherwise you wind up with something like: 0 - 100 = -100 insead of 100 - 0 = 100.
TofuBeer
Thanks for that, I thought I might have had it backwards...
Brendan
Use System.nanoTime(), currentTimeMillis() is error prone due to os interrupt rates: http://blogs.sun.com/dholmes/entry/inside_the_hotspot_vm_clocks
Ivo Wetzel
+1  A: 

There's a simple way to get total time of a routine as a percent of wall-clock execution time (rather than milliseconds). Just use ctrl-break to get a bunch of stackshots while you're waiting for it. The fraction of them containing the routine is the % of time it takes. The accuracy depends on how many shots you take. If you're just looking for where the problems are, you don't need precision time measurement. Here's a short explanation of how it works.

Mike Dunlavey
+3  A: 

Just take a snapshot of the profiling results. You will get the wall-clock time as well as self time there.

JB
A: 

If you don't want to add the start/end logs, try java assist/ Spring AOP it's really cool.

http://www.senthilb.com/2010/05/javaassist-byte-code-enhancement.html

Senthil Balakrishnan