I want to print the execution time for a method in java. Currently am using the following code,
long startTime = System.currentTimeMillis();
// method body
long runTime= System.currentTimeMillis() - startTime;
System.out.println(" XYZ Method execution time: " + runTime);
There are so many methods for which I would like to find the runtime in the project, is there any way to prevent writing this piece of code in every method?
The project uses Spring 2.5, Struts 2.
Edit:I do not want to add the logic to every method, I would like to, say, write the logic once and use some kind of configuration where I can specify the methods for which I need to print the execution time. So whenever the method is executed, automatically the run time should get printed to the console. One way is as said by me is to add the code to prologue and epilogue, but I do not want to edit the method as that would be time consuming and writing the same code wont be a good practice.
I recommend you take a look at the Metrics package from Codahale. This package collects various metrics (including timing) which is then reported via JMX or other mechanisms.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With