为什么在java中实现try catch块时处理速度会变慢?

时间:2015-06-07 15:56:01

标签: java

我一直在研究一个问题,

  

它只是从控制台逐行读取并找到该部分   其中包含一个模式并将其替换为实际数字

资源: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--;
        }
    }
}

我在使用120个测试用例的ideone实现它之后得到了这个

  

成功时间:0.14记忆:320832信号:0

之后,仅仅为了测试,我通过消除模式和匹配器完全改变并使用try catch代替

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

我无法弄清楚,为什么即使在删除了find方法的工作后,下一个实现中的时间也会增加。

是否因为尝试捕获,如果是,为什么会这样?

1 个答案:

答案 0 :(得分:0)

除了嵌套try catch之外,我们可以将内部try catch引入到外部catch块之后。我没有确切的理由认为try catch的嵌套不是一个好主意。