Sei sulla pagina 1di 9

Spesifikasi :

System Operasi

: Windows

Compiler

: Java: 1.7.0_01; Java HotSpot(TM) 64-Bit Server VM

21.1-b02
Processor

: Interl(R) Core(TM i5 CPU

Installed memory (RAM) :


System type

4. 00 GB (3.80 GB usable)

: 64-bit Operating System, x64-based processor


Source Code :

1.

Metode Nave

public class Standard {


private static int nilai = 125;
private static int[][] a = new int[nilai][nilai];
private static int[][] b = new int[nilai][nilai];
private static int[][] c = new int[nilai][nilai];
public static void naive(int[][] a, int[][] b)
{
int i,j;
for(i=0;i<a.length;i++)
{
for(j =0; j<a.length;j++)
{ c[i][j]=0;
for(int k = 0;k<a.length;k++)
{
c[i][j] += a[i][k] * b[k][j];
}
}
}
for(i=0;i<a.length;i++)
{

for(j =0; j<a.length;j++)


{
}
}
}
public static void main(String[] args) {
int i,j;
for(i=0;i<nilai;i++)
{
for(j =0; j<nilai;j++)
{
a[i][j] = 21;
b[i][j] = 21;
}
}
System.out.println("Perkalian Matriks : "+nilai+" x "+nilai);
double awal = System.currentTimeMillis();
naive(a,b);
double akhir = System.currentTimeMillis();
System.out.println("Waktu Eksekusi Program Metode Naive : "+(akhirawal)/1000.0+" Milisecond");
}
}

Hasil Eksekusi Metode Nave:

Matriks 125 x 125

Matriks 250 x 250

Matriks 512 x 512

Matriks 1024 x 1024

Matriks 2048 x 2048

2.

Metode Strassen

public class Strassen {


public static void main(String[] args) {
int nilai = 8;
int[][] a = new int[nilai][nilai];
int[][] b = new int[nilai][nilai];
int[][] c = new int[nilai][nilai];
int i,j;
for(i=0;i<nilai;i++)
{
for(j =0; j<nilai;j++)
{
a[i][j] = 21;
b[i][j] = 21;
}
}
System.out.println("Perkalian Matriks "+nilai+" x "+nilai);
double awal = System.currentTimeMillis();
strassen(a,b) ;
double akhir = System.currentTimeMillis();

System.out.println("Waktu Eksekusi Program : "+(akhir-awal)/1000.0+"


Milisecond");
System.gc();
}
public static int [][] strassen(int [][] a, int [][] b)
{
int n = a.length;
int [][] result = new int[n][n];
if(n <= 8 )
{
result[0][0] = a[0][0] * b[0][0];
}
else
{
int [][] A11 = new int[n/2][n/2];
int [][] A12 = new int[n/2][n/2];
int [][] A21 = new int[n/2][n/2];
int [][] A22 = new int[n/2][n/2];
int [][] B11 = new int[n/2][n/2];
int [][] B12 = new int[n/2][n/2];
int [][] B21 = new int[n/2][n/2];
int [][] B22 = new int[n/2][n/2];
divide(a, A11, 0 , 0);
divide(a, A12, 0 , n/2);
divide(a, A21, n/2, 0);
divide(a, A22, n/2, n/2);
divide(b, B11, 0 , 0);
divide(b, B12, 0 , n/2);

divide(b, B21, n/2, 0);


divide(b, B22, n/2, n/2);
int [][] P1 = strassen(add(A11, A22), add(B11,
B22));
int [][] P2 = strassen(add(A21, A22), B11);
int [][] P3 = strassen(A11, sub(B12, B22));
int [][] P4 = strassen(A22, sub(B21, B11));
int [][] P5 = strassen(add(A11, A12), B22);
int [][] P6 = strassen(sub(A21, A11), add(B11, B12));
int [][] P7 = strassen(sub(A12, A22), add(B21, B22));
int [][] C11 = add(sub(add(P1, P4), P5), P7);
int [][] C12 = add(P3, P5);
int [][] C21 = add(P2, P4);
int [][] C22 = add(sub(add(P1, P3), P2), P6);
copy(C11, result, 0 , 0);
copy(C12, result, 0 , n/2);
copy(C21, result, n/2, 0);
copy(C22, result, n/2, n/2);
}
return result;
}
public static int [][] add(int [][] A, int [][] B)
{
int n = A.length;
int [][] result = new int[n][n];
for(int i=0; i<n; i++)
for(int j=0; j<n; j++)
result[i][j] = A[i][j] + B[i][j];

return result;
}
public static int [][] sub(int [][] A, int [][] B)
{
int n = A.length;
int [][] result = new int[n][n];
for(int i=0; i<n; i++)
for(int j=0; j<n; j++)
result[i][j] = A[i][j] - B[i][j];
return result;
}
public static void divide(int[][] p1, int[][] c1, int iB, int jB)
{
for(int i1 = 0, i2=iB; i1<c1.length; i1++, i2++)
for(int j1 = 0, j2=jB; j1<c1.length; j1++, j2++)
{
c1[i1][j1] = p1[i2][j2];
}
}
public static void copy(int[][] c1, int[][] p1, int iB, int jB)
{
for(int i1 = 0, i2=iB; i1<c1.length; i1++, i2++)
for(int j1 = 0, j2=jB; j1<c1.length; j1++, j2++)
{
p1[i2][j2] = c1[i1][j1];
}
}
}
Hasil Eksekusi Metode Strassen :
Matriks 125 x 125

Matriks 250 x 250

Matriks 512 x 512

Matriks 1024 x 1024

Matriks 2048 x 2048

Tabel Perbandingan Waktu Eksekusi Program


NxN
125 x 125
250 x 250
512 x 512
1024 x

Metode Nave
0.016
0.051
0.511
10.276

Metode Strassen
0.025
0.101
0.546
3.205

1024
2048 x

135.411

21.797

2048

Potrebbero piacerti anche