EDIT: so, this happens only on android, on desktop results are pretty much the same, but on android ugly code is 10x faster. Tested on android 4.4 (samsung galaxy s4), android 8 (nexus 6p), android emulator on Mac.
After refactoring code of my android program I noticed, that method invocation is very performance costly. Lets say, I have a class
public class Chunk {
private byte[] chunkArray;
private ChunkGetter chunkGetter;
public Chunk() {
chunkArray = new byte[65536];
chunkGetter = new ChunkGetter();
}
public byte getByteFromArray(int x, int y, int z) {
return chunkGetter.getBlockId(x, y, z, chunkArray);
}
public byte[] getChunkArray() {
return chunkArray;
}
}
and a getter to get data from a chunk array:
public ChunkGetter() {
}
public byte getBlockId(int x, int y, int z, byte[] blocksByteArray) {
return blocksByteArray[getCoordinateOffset(x, y, z)];
}
public static int getCoordinateOffset(int x, int y, int z) {
return x * 256 * 16 + z * 256 + y;
}
So, a simple getting test gave me these results:
private void runTest() {
Chunk chunk = new Chunk();
long start = System.nanoTime();
for (int x = 0; x < 16; x++) {
for (int z = 0; z < 16; z++) {
for (int y = 0; y < 256; y++) {
byte id = chunk.getByteFromArray(x, y, z);
}
}
}
LOG("test took: " + (System.nanoTime() - start) / 1000000 + " ms");
}
first call: test took: 19 ms
second call: test took: 16 ms
third call: test took: 17 ms
But if I get data directly from the array - it is 20 times faster:
private void runTest() {
Chunk chunk = new Chunk();
byte[] chunkArray = chunk.getChunkArray();
long start = System.nanoTime();
for (int x = 0; x < 16; x++) {
for (int z = 0; z < 16; z++) {
for (int y = 0; y < 256; y++) {
byte id = chunkArray[x * 256 * 16 + z * 256 + y];
}
}
}
LOG("test took: " + (System.nanoTime() - start) / 1000000 + " ms");
}
first call: test took: 1 ms
second call: test took: 1 ms
third call: test took: 1 ms
This code is not readable and not flexible but when using it, my program runs init method in 1.5 sec and when using methods - it runs in 9 sec! How can I achieve good performance without ugly copy-pasting?