我有一个 func a(x, y z)
想要计时,有没有办法做到这一点:
time = timeit( a(x,y,z) )
在 java 中,它接受任何函数的时间。
喜欢:
long timeit(Object func){
long startTime = System.currentTimeMillis();
func(...);
return System.currentTimeMillis() - startTime;
}
最佳答案
您需要将您的函数放在 Runnable
接口(interface)中,在 public void run()
方法中。
修改代码:
public long timeit(Runnable func)
{
long startTime = System.currentTimeMillis();
func.run();
return System.currentTimeMillis() - startTime;
}
功能:
public class MyFunction implements Runnable
{
@Override public void run() { System.out.println(); }
}
关于java - 如何创建定时器功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18128963/