Answer:
Check the explanation
Explanation:
Code:
****************************************
import java.util.Random;
public class Matrices {
static int staticMatrix1[][] = new int [100][100];
static int staticMatrix2[][] = new int [100][100];
static int staticMatrix3[][] = new int [100][100];
public void localMatrices() {
int localMatrix1[][] = new int [100][100];
int localMatrix2[][] = new int [100][100];
int localMatrix3[][] = new int [100][100];
Random r = new Random();
for(int i =0;i<100;i++) {
for(int j =0;j<100;j++) {
localMatrix1[i][j] = r.nextInt(100);
localMatrix2[i][j] = r.nextInt(100);
}
}
long localStart = System.nanoTime();
multiplyLocal(localMatrix1, localMatrix2);
long localEnd = System.nanoTime();
System.out.println("Time taken for local multiplication: "+ (localEnd-localStart)/ 1000000);
}
public void multiplyLocal(int first[][],int second[][]) {
int[][] multiply = new int [100][100];
for (int c = 0; c < 100; c++)
{
for (int d = 0; d < 100; d++)
{
int sum = 0;
for (int k = 0; k < 100; k++)
{
sum = sum + first[c][k]*second[k][d];
}
multiply[c][d] = sum;
sum = 0;
}
}
}
public static void multiplyStatic(int first[][],int second[][]) {
int[][] multiply = new int [100][100];
for (int c = 0; c < 100; c++)
{
for (int d = 0; d < 100; d++)
{
int sum = 0;
for (int k = 0; k < 100; k++)
{
sum = sum + first[c][k]*second[k][d];
}
multiply[c][d] = sum;
sum = 0;
}
}
}
public static void main(String args[]) {
Random r = new Random();
for(int i = 0;i<100;i++) {
for(int j = 0;j<100;j++) {
staticMatrix1[i][j] = r.nextInt(100);
staticMatrix2[i][j] = r.nextInt(100);
}
}
long staticStart = System.nanoTime();
multiplyStatic(staticMatrix1, staticMatrix2);
long staticEnd = System.nanoTime();
System.out.println("Time taken for static multiplication: "+ (staticEnd-staticStart)/ 1000000);
Matrices matrices = new Matrices();
matrices.localMatrices();
}
}
***************************************
Outputs:
Time taken for static multiplication: 6
Time taken for local multiplication: 12
******************************************