我正在尝试测量执行时间,但我不知道该方法在try块之前还是在里面?
ublic static void main(String[] args) {
long startTime = System.currentTimeMillis();
try {
SearchDetailsDialog dialog = new SearchDetailsDialog(null);
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}long endTime = System.currentTimeMillis();
long totalTime = ((endTime - startTime)/1000);
System.out.println("the execution time is:");
System.out.println(totalTime);
}
答案 0 :(得分:1)
都不是。在你的情况下,最好的是:
long startTime= System.nanoTime() ;
try {
SearchDetailsDialog dialog = new SearchDetailsDialog(null);
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
} finally {
long endTime= System.nanoTime() ;
double totalTime= ((double)(endTime - startTime)/1000000000) ;
System.out.printf("the execution time is: %.3f seconds",totalTime);
}
答案 1 :(得分:0)
将此代码放在finally块中:
long endTime = System.currentTimeMillis();
long totalTime = ((endTime - startTime)/1000);
System.out.println("the execution time is:");
System.out.println(totalTime);
答案 2 :(得分:0)
同意nanoTime()如果可用,但我会在try和catch之间放置开始和停止时间捕获,该机制也会花费一些时间。或者你可以在两个点捕获停止时间(在catch块中第二个).. println()在任何地方......