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:
- This function is called in a loop with the intention of finding best match(es).
- Performance for a single iteration is critical as there are lot of iterations.
- 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.
- The above code takes anywhere between 200 - 600 ms.
- 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.
- If I remove "Math.abs" the performance improves bringing the average to 150 ms.
- 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:
- Why and how differently do Development server (Windows 7/eclipse/JDK6) and Deployment server behave in terms of performance tweaks?
- Is there any better algorithm to the match?
Am stumped. Any help appreciated.