I have the following method in a Java app:
public void setPixel(int x, int y, int rgb) {
if (isValidPixel(x, y)) {
bitmap[indexOf(x, y)] = rgb;
}
}
When I put direct calculations instead of the method calls, like:
public void setPixel(int x, int y, int rgb) {
if (x < width && y < height) {
bitmap[y * width + x] = rgb;
}
}
the code runs with 4 to 5 milliseconds more than the first. So why?
If your numbers are correct and reproducible, the most probable answer is that the JIT compiler manages to compile the method but gives up on the inline statement. You could disable the JIT compiler to prove the theory.