Method's execution time in java
java, performance, spring
Solution
Why don't you just use a profiler, like YourKit or JVisualVM. JVisualVM comes with the JDK. If you really want to do this yourself, use `java.lang.instrument` and `ASM` to write your own agent. It's simple to add logic to the prologue and epilogue of a Java method using this approach.
Here's a link to get you started.
Problem
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.