我有一个简单的类,我想测量方法调用时间,我该怎么做?寻找通用的方法来实现这一点,所以我可以将它应用于更困难的类。谢谢java我如何测量方法调用时间?
import java.io.*;
import java.util.*;
public class Turtle {
private String name;
private Formatter f;
public Turtle(String name, Formatter f) {
this.name = name;
this.f = f;
}
public void move(int x, int y) {
f.format("%s The Turtle is at (%d,%d)\n", name, x, y);
}
public static void main(String[] args) {
PrintStream outAlias = System.err;
Turtle tommy = new Turtle("Tommy",
new Formatter(System.out));
Turtle terry = new Turtle("Terry",
new Formatter(outAlias));
tommy.move(0,0);
terry.move(4,8);
tommy.move(3,4);
terry.move(2,5);
tommy.move(3,3);
terry.move(3,3);
}
}
你是什么意思,“方法通话时间”? – 2012-02-25 13:22:20
由于其中一个建议它是CPUT时间。我想知道是否有任何Java内置方法。不寻找任何复杂的,如探查器,但我可以在我的代码中使用ad hoc。像这样:long startTime = System.currentTimeMillis(); tommy.move(0,0); long endTime = System.currentTimeMillis(); System.out.println(“That took”+(endTime - startTime)+“milliseconds”); //但有办法用一些Java方法在一行中完成它? – aretai 2012-02-25 14:19:25
@aretal您的示例度量值是经过的(墙)时间,而不是CPU时间。显然这很难在一条线上,因为你至少需要标记时钟应该以何种方式开始和停止的线。方面可以给你一个紧凑的解决方案,但如果你想要测量时间,那么挖掘AOP可能是矫枉过正。 – 2012-02-26 17:57:34