سؤال

I am trying to measure the execution time, but i do not know where to but the method is before the try block or inside??

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);
}
هل كانت مفيدة؟

المحلول

Neither. In your case the best is:

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

نصائح أخرى

Put this code in a finally block :

long endTime   = System.currentTimeMillis();
    long totalTime = ((endTime - startTime)/1000);
    System.out.println("the execution time is:");
    System.out.println(totalTime);

Agreed with nanoTime() if available but I would put both start and stop time captures between try and catch, that mechanism eats some time too. Alternatively you can capture stop time at two points (second within catch block).. println() anywhere ...

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top