java - 如何将执行时间方法与 try 和 catch 一起使用

标签 java execution-time

我正在尝试测量执行时间,但我不知道在哪里,但该方法是在 try block 之前还是内部?

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);
}

最佳答案

都不是。根据您的情况,最好的是:

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);
}

关于java - 如何将执行时间方法与 try 和 catch 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18413315/

相关文章:

java - HttpPost 在请求正文中发布复杂的 JSONObject

java - 如何重新加载先前加载的在运行时更改的类?

java - 获取一个带有偏差的范围内的随机数

c++ - 按位移位的执行时间

java - 使用 WindowBuilder 动态创建 JButton

java - Android删除的联系人重新出现而WhatsApp联系人不出现

ruby-on-rails - Rails 集成测试 : reducing execution time

c++ - 为什么两个几乎相同的实现有很大的执行时间差异?

python - 数巴 : difference between first time execution and following executions

java - java中是否有一个命令来测量执行时间?