It's not a secret anymore!
The moose likes Java in General and the fly likes Time took for execution of methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Time took for execution of methods" Watch "Time took for execution of methods" New topic

Time took for execution of methods

vittal mareddy

Joined: Jul 24, 2011
Posts: 22

We have web application (servlets), need to find how much time took for execution of methods for user action, code is written without coding standards, without adding explicitly logger statement for each method; want to know time took for method execution. Please let me know how I can achieve.
Stephan van Hulst

Joined: Sep 20, 2010
Posts: 4616

1) Record starting time.
2) Execute method.
3) Record ending time.
4) Display ending time minus starting time.

The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.
vittal mareddy

Joined: Jul 24, 2011
Posts: 22
I have user action, it calls many method, i don't want to modify the code to add logger statement to display execution time, i am looking is there any framework i can use to get execution time for methods in user action.

fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11955

You may be able to use a profile to do what you are asking. I've never used one, so i couldn't say.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Alexander Kober
Ranch Hand

Joined: Aug 05, 2011
Posts: 32

Java profiling tools allow you to do what you described (among a lot of other things). You should note, however, that monitoring the JVM in this way will make it run a lot slower, so the timings you can achieve this way are not absolute values. You can, however, find out which methods require which fraction of the overall execution time, which in most cases is just as good.

A (imho very good) commercial tool with a lot of profiling options is JProfiler. A free alternative with less functions is the JVisualVM that is supplied with the Java SDK (in the bin/ subfolder).

The only way to get exact timings for your methods without writing extra code is through the use of java agents - this is, however, a fairly complicated topic and i guess the profiler option is better suited to your needs...
I agree. Here's the link:
subject: Time took for execution of methods
It's not a secret anymore!