How to see time taken by each method in Java program during execution

試著忘記壹切 提交于 2020-01-01 19:26:48

问题


I am trying to find the time taken by each method that my Java program calls, while executing. I do not want to litter my code with System.currentTimeMillis(). Maybe, something like a profiler or an IDE plugin, but I am not sure if that is what it is called. Can someone help me by pointing me in the right direction?

Sample code :

public static void main(String[] args){
   Obj A = new Obj();
   ObjDiff B = new ObjDiff();
   A.callMe();
   B.callMeToo();
}

Tools/Frameworks :

  • Eclipse
  • Struts

What I Want :

  1. Time taken to instantiate Obj A,
  2. Time taken to instantiate ObjDiff B,
  3. Time taken to run A.callMe(),
  4. Time taken to run B.callMeToo()

Any help will be greatly appreciated.

Cheers,
Rohitesh

EDIT :
I have installed VisualVM and used it as a standalone app. It has been helpful. However, my primary concern is still not addressed. I cannot look at the memory/CPU usage, method wise. Any thoughts on this?


回答1:


VisualVM is a good tool. My favortine one (by far) is JProfiler. It is not free and quite expensive if you're not a company but you can evaluate it for 10 days.




回答2:


I would recommend VisualVM too, basic but quite useful. Recommended you run it on the same Java 7 JVM as your app top avoid compatibility issues. If you need more advanced features like tracing a user interaction with a web server, you can look at more elaborate profilers like Introscope.



来源:https://stackoverflow.com/questions/16602503/how-to-see-time-taken-by-each-method-in-java-program-during-execution

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!