Last active
September 10, 2016 08:54
-
-
Save kravchik/75bf2ee8c78a4bcca4d320d91a8af195 to your computer and use it in GitHub Desktop.
Unswer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Round 1: unoptimized version | |
// 1.685 | |
// 1.633 | |
// 1.669 | |
// 1.667 | |
// | |
// Round 2: unoptimized version (again!) | |
// 1.684 | |
// 1.693 | |
// 1.668 | |
// 1.666 | |
// 1.665 | |
// 1.663 | |
// 1.665 | |
// 1.664 | |
// 1.663 | |
// 1.664 | |
// | |
// Round 3: optimized version | |
// 1.292 | |
// 1.284 | |
// 1.282 | |
// 1.281 | |
// 1.28 | |
// 1.281 | |
// 1.281 | |
// 1.279 | |
// 1.28 | |
// 1.28 | |
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"); | |
for (int k = 0; k < 4; k++) { | |
m2a(array1, array2, array3); | |
} | |
System.out.println(); | |
System.out.println("Round 2: unoptimized version (again!)"); | |
for (int k = 0; k < 10; k++) { | |
m2a(array1, array2, array3); | |
} | |
System.out.println(); | |
System.out.println("Round 3: optimized version"); | |
for (int k = 0; k < 10; k++) { | |
m3a(array1, array2, array3); | |
} | |
} | |
private static void m3a(float[] array1, float[] array2, float[] array3) { | |
long start = System.currentTimeMillis(); | |
for (int i = 0; i < 100000000; i++) O4Matrix.multiply33b(array1, array2, array3); | |
System.out.println((System.currentTimeMillis() - start) / 1e3f); | |
} | |
private static void m2a(float[] array1, float[] array2, float[] array3) { | |
long start = System.currentTimeMillis(); | |
for (int i = 0; i < 100000000; i++) O4Matrix.multiply33(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 multiply33c(float[] ab, float[] a, float[] b) { | |
for (int i = 1; i <= 3; i++) { | |
for (int j = 1; j <= 3; j++) { | |
float r = 0; | |
for (int k = 1; k <= 3; k++) r += get(3, a, i, k) * get(3, b, k, j); | |
set(3, 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