2

I have been working on a problem,

Its simply reading line by line from the console and find the part which contains a pattern and replace it with actual number

Resource: spoj

First i tried using finding the pattern of the text and replacing it with the actual number either by adding or subtracting as per required

My Code

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

and i got this after implementing it at ideone with 120 test cases

Success time: 0.14 memory: 320832 signal:0

After that, just for testing, i changed completely by eliminating pattern and matcher and used try catch instead

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

and i get this as result with the same input

Success time: 0.15 memory: 320832 signal:0

I can't figure it out, why the time got increased in the next implementation even after removing the find method job.

Does it because of try-catch, and if yes, why is it so?

Aman Singh
  • 743
  • 1
  • 10
  • 20
  • 2
    [This post](http://stackoverflow.com/questions/8255878/try-catch-performance-java) may be very helpful. – Mordechai Jun 07 '15 at 15:59
  • I don't think try catch block is responsible for that. – Thusitha Thilina Dayaratne Jun 07 '15 at 16:01
  • 1
    I assume the time is measured in seconds. Then this difference is only 1/100th of a second. This is not something to get worried over. It is just most likely an artifact of when start and stop times were recorded. – Brett Walker Jun 07 '15 at 16:04
  • You can't measure the performance of a Java program that way... Your results depend on so many factors that you can't conclude anything from them... – assylias Jun 07 '15 at 16:17

1 Answers1

0

Instead of nesting try catch, we can bring out inner try catch to subsequent to outer catch block. I don't have the exact reason that nesting of try catch would not be a good idea.

dvrnaidu
  • 151
  • 1
  • 1
  • 9