简体   繁体   English

如何在执行期间查看Java程序中每个方法所花费的时间

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

I am trying to find the time taken by each method that my Java program calls, while executing. 我试图找到我的Java程序在执行时调用的每个方法所花费的时间。 I do not want to litter my code with System.currentTimeMillis() . 我不想在System.currentTimeMillis()我的代码。 Maybe, something like a profiler or an IDE plugin, but I am not sure if that is what it is called. 也许,像分析器或IDE插件,但我不确定这是否是它的名称。 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 , 实例化Obj A时间,
  2. Time taken to instantiate ObjDiff B , 实例化ObjDiff B时间,
  3. Time taken to run A.callMe() , 运行A.callMe()所花费的时间,
  4. Time taken to run B.callMeToo() 运行B.callMeToo()时间B.callMeToo()

Any help will be greatly appreciated. 任何帮助将不胜感激。

Cheers, 干杯,
Rohitesh Rohitesh

EDIT : 编辑
I have installed VisualVM and used it as a standalone app. 我已经安装了VisualVM并将其用作独立应用程序。 It has been helpful. 它很有帮助。 However, my primary concern is still not addressed. 但是,我仍然没有解决我的主要问题。 I cannot look at the memory/CPU usage, method wise. 方法明智的我无法查看内存/ CPU使用情况。 Any thoughts on this? 有什么想法吗?

I would recommend VisualVM too, basic but quite useful. 我也会推荐VisualVM ,基本但非常有用。 Recommended you run it on the same Java 7 JVM as your app top avoid compatibility issues. 建议您在与应用程序顶部相同的Java 7 JVM上运行它,以避免兼容性问题。 If you need more advanced features like tracing a user interaction with a web server, you can look at more elaborate profilers like Introscope. 如果您需要更多高级功能,例如跟踪用户与Web服务器的交互,您可以查看更精细的分析器,如Introscope。

VisualVM is a good tool. VisualVM是一个很好的工具。 My favortine one (by far) is JProfiler. 我最喜欢的(到目前为止)是JProfiler。 It is not free and quite expensive if you're not a company but you can evaluate it for 10 days. 如果您不是公司,那么它不是免费且非常昂贵的,但您可以评估它10天。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM