Shubham Arawkar
Shubham Arawkar

Reputation: 61

Speed in java why some codes run faster

I was Attempting UVa problem number 10324 and I coded the following solution this gets an accepted but gives a really bad runtime of 2.670 seconds I have the following two codes This is my code

    public static void main(String[] args) throws Exception{
        // write your code here
        StringBuilder op = new StringBuilder();
            InputStreamReader isr = new InputStreamReader(System.in);
            BufferedReader reader = new BufferedReader(isr);
            String input, tmp[];
            int n, l, u, j, i = 0;
            boolean dec;
            char thi;
            while ((input = reader.readLine()) != null) {
                op.append("Case " + (++i) + ":\n");
                n = Integer.parseInt(reader.readLine());
                while (n-- > 0) {
                    tmp = reader.readLine().split(" ");
                    l = Integer.parseInt(tmp[0]);
                    u = Integer.parseInt(tmp[1]);
                    if (l > u) {
                        l ^= u;
                        u ^= l;
                        l ^= u;
                    }
                    //System.out.println(l + "|" + u);
                    dec = true;
                    thi = input.charAt(l++);
                    for (; l <= u; l++) {
                        if (thi != input.charAt(l)||(thi != input.charAt(u--))) {
                            dec = false;
                            break;
                        }
                    }
                    op.append((dec ? "Yes\n" : "No\n"));
                }
            }
            System.out.print(op.toString());
            return;

    }
}

and the other code is one I found off the Mr Gorgon's Solution This has a runtime of 0.84 seconds

public static void main(String[] args) throws Exception {
    InputStreamReader isr = new InputStreamReader(System.in);
    BufferedReader br = new BufferedReader(isr);
    StringBuilder sb = new StringBuilder("");
    int testCase = 1;
    String line;
    while ((line = br.readLine()) != null) {
        sb.append("Case ").append(testCase).append(":\n");
        testCase++;
        int noOfCases = Integer.parseInt(br.readLine());
        for (int j = 0; j < noOfCases; j++) {
            String[] str = br.readLine().split(" ");
            int val1 = Integer.parseInt(str[0]);
            int val2 = Integer.parseInt(str[1]);
            if (val1 > val2) {
                val1 ^= val2;
                val2 ^= val1;
                val1 ^= val2;
            }
            boolean isValid = true;
            if (val1 != val2) {
                for (int i = val1; i < val2; i++) {
                    if (line.charAt(i) != line.charAt(i + 1)) {
                        isValid = false;
                        break;
                    }
                }
            }
            if (isValid)
                sb.append("Yes\n");
            else
                sb.append("No\n");
        }
    }
    System.out.print(sb);
}

I found it extremely hard to understand as to why this code runs so much faster when all the tasks are essentially same.and my code has lesser declarations than Gorgons code

Upvotes: 3

Views: 136

Answers (3)

Shubham Arawkar
Shubham Arawkar

Reputation: 61

its the

input.charAt(l)!=input.charAt(l+1)

line which helps to speed up the program i think it has to do with caching

I researched and found something called JCS which probably allows their apache server to ache the memory data and helps in faster access

Upvotes: 2

Vineet Kasat
Vineet Kasat

Reputation: 1014

Below piece of code is missing in your solution. You are looping blindly where as if condition limits looping.

if (val1 != val2) {
                                    }
            }

Upvotes: -1

Scary Wombat
Scary Wombat

Reputation: 44854

  • op.append("Case " + (++i) + ":\n");

This negates the benefits of StringBuilder

  • if (val1 != val2) { In the second version shortcuts the looping

Upvotes: 2

Related Questions