2015-06-07 30 views
2

我一直對一個問題,爲什麼處理在java中實現try catch塊會變慢?

它只是從控制檯讀取一行行,並找到其中包含一個模式的一部分 與實際數量更換

資源:spoj

首先我試圖找到文本的模式,並用實際的數字替換它,或者根據需要添加或減去

我的代碼

import java.io.BufferedReader; 
import java.io.InputStreamReader; 
import java.io.IOException; 
import java.util.regex.Pattern; 
import java.util.regex.Matcher; 


class ABSYS { 
    public static void main(String[] args) throws IOException { 
     int t; 
     String[] numArray = new String[2]; 
     String[] numArray2 = new String[2]; 
     BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); 
     t = Integer.parseInt(reader.readLine()); 
     while(t > 0) { 
      String input = reader.readLine(); 
      if(input.isEmpty()) { 
       continue; 
      } 
      numArray = input.split("\\s{1}=\\s{1}"); 
      numArray2 = numArray[0].split("\\s{1}\\+\\s{1}"); 
      Pattern pattern = Pattern.compile("machula"); 
      Matcher matcher = pattern.matcher(numArray[1]); 
      if(matcher.find()) { 
       System.out.println(numArray[0] + " = " + (Integer.parseInt(numArray2[0]) + Integer.parseInt(numArray2[1]))); 
      } 
      else { 
       matcher = pattern.matcher(numArray2[0]); 
       if(matcher.find()) { 
        System.out.println((Integer.parseInt(numArray[1]) - Integer.parseInt(numArray2[1])) + " + " + numArray2[1] + " = " + numArray[1]); 
       } 
       else { 
        System.out.println(numArray2[0] + " + " + (Integer.parseInt(numArray[1]) - Integer.parseInt(numArray2[0])) + " = " + numArray[1]); 
       } 
      } 
      t--; 
     } 
    } 
} 

和我在ideone 120測試用例

實施之後得到這個

成功時間:0.14內存:320832信號:0

之後,只需進行測試,我通過消除模式,匹配和使用嘗試捕捉徹底改變,而不是

import java.io.BufferedReader; 
import java.io.InputStreamReader; 
import java.io.IOException; 

class ABSYS { 
    public static void main(String[] args) throws IOException { 
     int t; 
     String[] numArray = new String[2]; 
     String[] numArray2 = new String[2]; 
     BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); 
     t = Integer.parseInt(reader.readLine()); 
     while(t > 0) { 
      String input = reader.readLine(); 
      if(input.isEmpty()) { 
       continue; 
      } 
      numArray = input.split("\\s{1}=\\s{1}"); 
      numArray2 = numArray[0].split("\\s{1}\\+\\s{1}"); 
      int a = 0, b = 0, c = 0; 
      try { 
       c = Integer.parseInt(numArray[1]); 
       try { 
        a = Integer.parseInt(numArray2[0]); 
        System.out.println(a+" + "+(c - a)+" = "+c); 
       } 
       catch(NumberFormatException nfe) { 
        b = Integer.parseInt(numArray2[1]); 
        System.out.println((c - b)+" + "+b+" = "+c); 
       } 
      } 
      catch(NumberFormatException nfe) { 
       System.out.println(numArray[0] + " = " + (Integer.parseInt(numArray2[0]) + Integer.parseInt(numArray2[1]))); 
      } 
      t--; 
     } 
    } 
} 

和我得到這個結果作爲以相同的輸入

成功時間:0.15內存:320832信號:0

我無法弄清楚,爲什麼時間得到了在未來增加即使在刪除查找方法作業之後仍然可以執

它,因爲的try-catch的,如果是,爲什麼會這樣呢?

+2

本帖](http://stackoverflow.com/questions/8255878/try-catch-performance-java)可以是非常有益的。 – Mordechai

+0

我不認爲try catch塊對此負責。 –

+1

我假設時間以秒爲單位。那麼這個差距只有1/100秒。這並不值得擔心。這很可能是記錄開始和停止時間的人爲因素。 –

回答

0

代替嵌套嘗試捕捉,我們可以帶出內嘗試捕捉到隨後到外catch塊。我沒有確切的理由,試圖捕捉並不是一個好主意。