一段測試try...catch執行時間的程式碼

劍握在手發表於2013-11-09

public class Test2 {

    private static int test() {
        int i=1;

        try {
            i=2;
            return i;
        } catch (Exception e) {
            i = 3;
            return i;
        } finally {
            i = 4;
            System.out.println(i);
        }
       
    }

    public static void main(String[] args){
     double b = 0;
     String s = "";
     long ii = System.currentTimeMillis();
        for(int i = 0; i<20000;i++) {
         try {
          b = StrictMath.pow(b+i, 1.0/3);
          s = s + b+";";
         }catch(Exception e){
          e.printStackTrace();
         }finally {

         }
        }
       
        //System.out.println("b = "+b + "\ns = "+ s);
        System.out.println(System.currentTimeMillis()-ii);
       
     double c = 0;
     String ss = "";
     long iii = System.currentTimeMillis();
        for(int i = 0; i<20000;i++) {
          c = StrictMath.pow(c+i, 1.0/3);
          ss = ss + c+";";
        }
       
        //System.out.println("b = "+c + "\nss = "+ ss);
        System.out.println(System.currentTimeMillis()-iii);
       
    }
}

相關文章