Sum the difference of two arrays
It's hard to say anything without profiling the whole application. Two possible scenarios about this point:


  If I were to not have any logic in this code and directly return any integer, my overall code takes 100 ms to complete on an average.


#1

I guess the application processes the output of patternMatch, so maybe it handles different results differently which varies in time. Consider the following example:

public static int patternMatch(int [] pattern1, int [] pattern2, int range) {
    return 5;
}

int result = patternMatch( [] pattern1, int [] pattern2, int range) {
if (result &lt; 10) {
    // do nothing
} else {
    // do something complicated and slow
}


Of course, it might not be so obvious. Profile it!

#2

JMV is smart about optimizations. If you put the simple return 5 statement into the method (as above) the JVM might have fooled you and did not even call the patternMatch method and probably eliminated or optimized other codes too which call patternMatch.


  For example, reconsider the code in Listing 4: note 
  that main not only computes result but also uses result
  in the output that it prints. Suppose that I make just
  one tiny change and remove result from the println. In 
  this case, an aggressive compiler might conclude that it 
  does not need to compute result at all. 


Source: Robust Java benchmarking, Part 1: Issues



I've created a test class for testing the runtime. It takes only 8,766 ms on my machine.

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import org.junit.Test;

import com.google.common.base.Stopwatch;

public class AppEngineCompareTest {

    private final Random random = new Random();

    @Test
    public void test() {
        final Stopwatch stopwatch = Stopwatch.createStarted();
        final int count = 20000;
        final int[] ranges = createRandomIntegerArray(count);
        final List&lt;int[]&gt; patternOneList = createPatterns(count);
        final List&lt;int[]&gt; patternTwoList = createPatterns(count);
        System.out.println("gen: " + stopwatch);
        stopwatch.reset();
        stopwatch.start();
        int result = 0;
        for (int index = 0; index &lt; count; index++) {
            final int[] pattern1 = patternOneList.get(index);
            final int[] pattern2 = patternTwoList.get(index);
            final int range = ranges[index];
            result += AppEngineCompare.patternMatch(pattern1, pattern2, range);
        }
        System.out.println("run: " + stopwatch);
        System.out.println(result);
    }

    private int[] createRandomIntegerArray(final int count) {
        final int[] ranges = new int[count];
        for (int index = 0; index &lt; count; index++) {
            ranges[index] = random.nextInt();
        }
        return ranges;
    }

    private List&lt;int[]&gt; createPatterns(final int count) {
        final List&lt;int[]&gt; result = new ArrayList&lt;&gt;(count);
        for (int index = 0; index &lt; count; index++) {
            final int[] patterns = createRandomIntegerArray(60);
            result.add(patterns);
        }
        return result;
    }
}