Speed in java why some codes run faster
<p>I was Attempting UVa problem number <a href="https://uva.onlinejudge.org/index.php?option=onlinejudge&amp;page=show_problem&amp;problem=1265" rel="nofollow">10324</a>
and I coded the following solution this gets an accepted but gives a really <strong>bad runtime of 2.670 seconds</strong> 
I have the following two codes
This is my code</p>

<pre><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-- &gt; 0) {
                    tmp = reader.readLine().split(" ");
                    l = Integer.parseInt(tmp[0]);
                    u = Integer.parseInt(tmp[1]);
                    if (l &gt; u) {
                        l ^= u;
                        u ^= l;
                        l ^= u;
                    }
                    //System.out.println(l + "|" + u);
                    dec = true;
                    thi = input.charAt(l++);
                    for (; l &lt;= 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;

    }
}
</code></pre>

<p>and the other code is one I found off the <a href="http://kapelosh.blogspot.in/2013/10/10324-zeros-and-ones-java.html" rel="nofollow">Mr Gorgon's Solution</a>
This has a <strong>runtime of 0.84 seconds</strong></p>

<pre><code>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 &lt; noOfCases; j++) {
            String[] str = br.readLine().split(" ");
            int val1 = Integer.parseInt(str[0]);
            int val2 = Integer.parseInt(str[1]);
            if (val1 &gt; val2) {
                val1 ^= val2;
                val2 ^= val1;
                val1 ^= val2;
            }
            boolean isValid = true;
            if (val1 != val2) {
                for (int i = val1; i &lt; 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);
}
</code></pre>

<p>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</p>