im working on performance-differences between Android JAVA- and Android NDK-applications.
I performed a Matrix4D-Vector4D Transformation on more than 90000 vertices as an example for 3D Graphics.
It seemes, that the JAVA Version is nearly 100 times slower than the C-Version. Did i something wrong? Does anyone have similar experiences?
my Java-Code for transformation:
long t1 = System.nanoTime();
for ( int i = 0; i < vCount; i++)
{
Vector4 vOut = new Vector4();
Vector4 v = vertices[i];
vOut.v_[0] = v.v_[0] * matrix[0].v_[0];
vOut.v_[1] = v.v_[0] * matrix[0].v_[1];
vOut.v_[2] = v.v_[0] * matrix[0].v_[2];
vOut.v_[3] = v.v_[0] * matrix[0].v_[3];
vOut.v_[0] += v.v_[1] * matrix[1].v_[0];
vOut.v_[1] += v.v_[1] * matrix[1].v_[1];
vOut.v_[2] += v.v_[1] * matrix[1].v_[2];
vOut.v_[3] += v.v_[1] * matrix[1].v_[3];
vOut.v_[0] += v.v_[2] * matrix[2].v_[0];
vOut.v_[1] += v.v_[2] * matrix[2].v_[1];
vOut.v_[2] += v.v_[2] * matrix[2].v_[2];
vOut.v_[3] += v.v_[2] * matrix[2].v_[3];
vOut.v_[0] += v.v_[3] * matrix[3].v_[0];
vOut.v_[1] += v.v_[3] * matrix[3].v_[1];
vOut.v_[2] += v.v_[3] * matrix[3].v_[2];
vOut.v_[3] += v.v_[3] * matrix[3].v_[3];
vertices[i] = vOut;
}
long t2 = System.nanoTime();
long diff = t2 - t1;
double ms = (double)(diff / 1000000.0f);
Log.w("GL2JNIView", String.format("ms %.2f ", ms));
Performance (Transform > 90 000 Vertices | Android 4.0.4 SGS II):
(Median-value of 200 runs)
JAVA-Version: 2 FPS
C-Version: 190 FPS
You create a new Vector4 in each Iteration. From my own experience using new inside loops can cause unexpected performance problems in Android.