Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I have a function in App Engine java where I compare two patterns stored in an int array.

Below is the code:

public static int patternMatch(int [] pattern1, int [] pattern2, int range) {

    int max = range * pattern1.length;

    int match = (pattern1.length - pattern2.length) * range;

    for(int i = 0; i < pattern2.length; i++) {
        match += Math.abs(pattern1[i] - pattern2[i]);
    }

    return (max - match) * 100 / max;
}

I am facing very weird problems with respect to performance of this function between the development server and deployment on app engine as listed below:

  1. This function is called in a loop with the intention of finding best match(es).
  2. Performance for a single iteration is critical as there are lot of iterations.
  3. 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.
  4. The above code takes anywhere between 200 - 600 ms.
  5. On the development server, if I replace "int match = (pattern1.length - pattern2.length) * range;" by "int match = Math.abs(pattern1.length - pattern2.length) * range;", somehow the performance improves bringing the time taken down to 200 - 300 ms only. No impact on deployment server.
  6. If I remove "Math.abs" the performance improves bringing the average to 150 ms.
  7. I tried replacing Math.abs with bit operations to derive absolute value. I see huge performance improvement on development server ~160 ms. On deployment server it makes things worse ~700 ms.

What I would like to know here is:

  1. Why and how differently do Development server (Windows 7/eclipse/JDK6) and Deployment server behave in terms of performance tweaks?
  2. Is there any better algorithm to the match?

Am stumped. Any help appreciated.

share|improve this question
Maybe the development server runs in client mode while the deployment runs in client mode. stackoverflow.com/questions/198577/… – palacsint Nov 15 '12 at 19:05
Didn't get it. Your comment says both running in client mode. Either way if you meant one is running in client mode and another in server, then it should actually run faster on the server. – 0n4li Nov 16 '12 at 5:42
Did you check how much CPU/memory you get from App Engine? developers.google.com/appengine/docs/adminconsole/… – avip Nov 16 '12 at 6:39
It would help to have more info. How many iterations? What is the range typically? How large are the arrays? Is pattern1 always the same? You might be overflowing an int if the range and iterations are both large for example. You also may be better unrolling the abs ( diff = b - a; match = (diff < 0) ? -diff : diff) if the compiler doesn't. – Tim Niblett Nov 16 '12 at 14:50
Iterations are close to 20000 in one run. Range is < 100. Arrays are max 60 size. Pattern 1 in one run will be the same. – 0n4li Nov 18 '12 at 14:52
show 1 more comment

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.