Jas*_*son 5 java performance-testing
Java中是否有任何东西可以让我获取代码snippit并允许我确切地查看执行它需要多少"滴答".我想证明我写的算法比另一个更快.
"蜱虫"?不.我建议您每次运行几次并比较平均结果:
public class AlgorithmDriver {
public static void main(String [] args) {
int numTries = 1000000;
long begTime = System.currentTimeMillis();
for (int i = 0; i < numTries; ++i) {
Algorithm.someMethodCall();
}
long endTime = System.currentTimeMillis();
System.out.printf("Total time for %10d tries: %d ms\n", numTries, (endTime-begTime));
}
}
Run Code Online (Sandbox Code Playgroud)