Skip to content

Instantly share code, notes, and snippets.

@gunavaran
Created December 8, 2020 06:04
Show Gist options
  • Save gunavaran/91c15aba9f39d58c83076fdfa191b859 to your computer and use it in GitHub Desktop.
Save gunavaran/91c15aba9f39d58c83076fdfa191b859 to your computer and use it in GitHub Desktop.
import java.util.Random;
public class MatrixMultiplication {
static int n = 1024;
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) {
//populate the matrices with random values between 0.0 and 1.0
Random r = new Random();
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
A[i][j] = r.nextDouble();
B[i][j] = r.nextDouble();
C[i][j] = 0;
}
}
long start = System.nanoTime();
//matrix multiplication
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 timeDiff = (stop - start) * 1e-9;
System.out.println("Elapsed time in seconds: " + timeDiff);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment