Why these two pieces of codes, running on same conditions, does not have difference in amount of running time?
Code 1
static long time = 0;
static int n = 200;
static float[] vq = new float[200 * 200 * 200 * 3];
static int[] iq = new int[200 * 200 * 200];
static FloatBuffer verts = BufferUtils.createFloatBuffer(vq.length);
static IntBuffer ind = BufferUtils.createIntBuffer(iq.length);
static void draw() {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
for (int k = 0; k < n; k++) {
int index = (i * n * n) + (j * n) + k;
vq[3 * index + 0] = i;
vq[3 * index + 1] = j;
vq[3 * index + 2] = k;
iq[index] = index;
}
}
}
verts.put(vq);
ind.put(iq);
verts.flip();
ind.flip();
GL11.glEnableClientState(GL11.GL_VERTEX_ARRAY);
GL11.glVertexPointer(3, 0, verts);
GL11.glDrawElements(GL11.GL_QUADS, ind);
GL11.glDisableClientState(GL11.GL_VERTEX_ARRAY);
long newTime = System.currentTimeMillis();
System.out.println(newTime - time);
time = newTime;
}
Code 2
static int n = 200;
static long time = 0;
static void draw() {
GL11.glBegin(GL11.GL_QUADS);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
for (int k = 0; k < n; k++) {
GL11.glVertex3f(i, j, k);
}
}
}
GL11.glEnd();
long newTime = System.currentTimeMillis();
System.out.println(newTime - time);
time = newTime;
}
The reason I think Code 2 should be slower than Code1, is that it has near 8 millions native calls, but Code 1 has just a few, and they do the same thing in result.
What is the reason, and how could I improve performance of my code?
Code 1 includes copying your values into an array and then copying the array into native memory – if your n stays the same you can avoid this overhead by only doing it once.