Skip to content

Instantly share code, notes, and snippets.

@kravchik
Last active September 10, 2016 07:26
Show Gist options
  • Save kravchik/d8ccc44dfa94139ca2aced521af290f6 to your computer and use it in GitHub Desktop.
Save kravchik/d8ccc44dfa94139ca2aced521af290f6 to your computer and use it in GitHub Desktop.
Mystery of the JVM
// Round 1: unoptimized version
// why is here first cycle is too short and rest are too long?
// 1.639
// 3.333
// 3.298
// 3.289
//
// Round 2: unoptimized version (again!)
// why each cycle takes longer than first cycle of first round?
// and less than others?
// 1.975
// 1.977
// 1.977
// 1.983
// 1.976
// 1.975
// 1.974
// 1.974
// 1.983
// 1.982
//
// Round 3: optimized version
// 1.523
// 1.522
// 1.531
// 1.526
// 1.538
// 1.532
// 1.522
// 1.52
// 1.52
// 1.52
public class O4Matrix {
public static void main(String[] args) {
float[] array1 = new float[9];
float[] array2 = new float[9];
float[] array3 = new float[9];
System.out.println("Round 1: unoptimized version");
System.out.println(" why is here first cycle is too short and rest are too long?");
for (int k = 0; k < 4; k++) {
long start = System.currentTimeMillis();
for (int i = 0; i < 100000000; i++) O4Matrix.multiply33(array1, array2, array3);
System.out.println((System.currentTimeMillis() - start) / 1e3f);
}
System.out.println();
System.out.println("Round 2: unoptimized version (again!)");
System.out.println(" why each cycle takes longer than first cycle of first round?");
System.out.println(" and less than others?");
for (int k = 0; k < 10; k++) {
long start = System.currentTimeMillis();
for (int i = 0; i < 100000000; i++) O4Matrix.multiply33(array1, array2, array3);
System.out.println((System.currentTimeMillis() - start) / 1e3f);
}
System.out.println();
System.out.println("Round 3: optimized version");
for (int k = 0; k < 10; k++) {
long start = System.currentTimeMillis();
for (int i = 0; i < 100000000; i++) O4Matrix.multiply33b(array1, array2, array3);
System.out.println((System.currentTimeMillis() - start) / 1e3f);
}
}
public O4Matrix() {
}
public static void set(int ww, float[] data, int row, int column, float d) {
data[(row - 1) * ww + column - 1] = d;
}
public static float get(int ww, float[] data, int row, int column) {
return data[(row - 1) * ww + column - 1];
}
public static void multiply(int w, int h, float[] ab, float[] a, float[] b) {
for (int i = 1; i <= w; i++) {
for (int j = 1; j <= h; j++) {
float r = 0;
for (int k = 1; k <= h; k++) r += get(w, a, i, k) * get(w, b, k, j);
set(w, ab, i, j, r);
}
}
}
public static void multiply33(float[] ab, float[] a, float[] b) {
multiply(3, 3, ab, a, b);
}
public static void multiply33b(float[] ab, float[] a, float[] b) {
ab[0] = a[0] * b[0] + a[1] * b[3] + a[2] * b[6];
ab[1] = a[0] * b[1] + a[1] * b[4] + a[2] * b[7];
ab[2] = a[0] * b[2] + a[1] * b[5] + a[2] * b[8];
ab[3] = a[3] * b[0] + a[4] * b[3] + a[5] * b[6];
ab[4] = a[3] * b[1] + a[4] * b[4] + a[5] * b[7];
ab[5] = a[3] * b[2] + a[4] * b[5] + a[5] * b[8];
ab[6] = a[6] * b[0] + a[7] * b[3] + a[8] * b[6];
ab[7] = a[6] * b[1] + a[7] * b[4] + a[8] * b[7];
ab[8] = a[6] * b[2] + a[7] * b[5] + a[8] * b[8];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment