Skip to content

Instantly share code, notes, and snippets.

@karngyan
Created July 12, 2020 00:06
Show Gist options
  • Save karngyan/1f705f76386fa9a77ab87df40c5af3df to your computer and use it in GitHub Desktop.
Save karngyan/1f705f76386fa9a77ab87df40c5af3df to your computer and use it in GitHub Desktop.
import java.util.Random;
public class MatrixMultiply {
static int n = 4096;
static double[][] A = new double[n][n];
static double[][] B = new double[n][n];
static double[][] C = new double[n][n];
public static void main(String[] args) {
Random random = new Random();
for (int i = 0 ; i < n ; ++i) {
for (int j = 0 ; j < n ; ++j) {
A[i][j] = random.nextDouble();
B[i][j] = random.nextDouble();
C[i][j] = 0;
}
}
long start = System.nanoTime();
for (int i = 0 ; i < n ; ++i) {
for (int j = 0 ; j < n ; ++j) {
for (int k = 0 ; k < n ; ++k) {
C[i][j] = A[i][k] * B[k][j];
}
}
}
long stop = System.nanoTime();
double timeDifference = (stop - start) * 1e-9;
System.out.println(timeDifference + " seconds");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment