Initial commit 12.1

master
Lisa 7 years ago
parent 6063ca4b76
commit feba60b41e

@ -0,0 +1,26 @@
package _12._1;
import provided._12._1.Fibonacci;
import java.util.HashMap;
import java.util.Map;
public class FibonacciDynamic extends Fibonacci {
protected Map<Integer, Long> map = new HashMap<>();
public FibonacciDynamic() {
this.map.put(0, (long) 0);
this.map.put(1, (long) 1);
}
@Override
public long calculate(int n) {
if (this.map.containsKey(n)) {
return this.map.get(n);
} else {
long result = calculate(n-1) + calculate(n-2);
this.map.put(n, result);
return result;
}
}
}

@ -0,0 +1,47 @@
package _12._1;
public class FibonacciDynamicParallel extends FibonacciDynamic {
@Override
public long calculate(int n) {
if (this.map.containsKey(n)) {
return this.map.get(n);
} else {
FibonacciThread thread1 = new FibonacciThread(n-1);
FibonacciThread thread2 = new FibonacciThread(n-2);
thread1.start();
thread2.start();
try {
thread1.join();
thread2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
long result = thread1.getResult() + thread2.getResult();
this.map.put(n, result);
return result;
}
}
class FibonacciThread extends Thread {
private int n;
private long result;
public FibonacciThread(int n) {
this.n = n;
}
@Override
public void run() {
result = calculate(n);
}
public long getResult() {
return result;
}
}
}

@ -0,0 +1,50 @@
package _12._1;
import provided._12._1.Fibonacci;
public class FibonacciParallel extends Fibonacci {
@Override
public long calculate(int n) {
if (n == 0) {
return 0;
} else if (n == 1) {
return 1;
} else {
FibonacciThread thread1 = new FibonacciThread(n-1);
FibonacciThread thread2 = new FibonacciThread(n-2);
thread1.start();
thread2.start();
try {
thread1.join();
thread2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
return thread1.getResult() + thread2.getResult();
}
}
class FibonacciThread extends Thread {
private int n;
private long result;
public FibonacciThread(int n) {
this.n = n;
}
@Override
public void run() {
result = calculate(n);
}
public long getResult() {
return result;
}
}
}

@ -0,0 +1,17 @@
package _12._1;
import provided._12._1.Fibonacci;
public class FibonacciRecursive extends Fibonacci {
@Override
public long calculate(int n) {
if (n == 0) {
return 0;
} else if (n == 1) {
return 1;
} else {
return calculate(n-1) + calculate(n-2);
}
}
}

@ -0,0 +1,81 @@
package _12._1;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
class FibonacciTest {
private static final int[] NUMBERS = new int[] { 3, 5, 8, 12, 9, 18, 15, 10, 7, 11, 20 };
@Test
void test() {
int count = 0;
long start;
long stop;
long time;
start = System.nanoTime();
FibonacciRecursive r = new FibonacciRecursive();
long[] rResult = new long[NUMBERS.length];
for (int i : NUMBERS) {
rResult[count] = r.calculate(i);
count++;
}
stop = System.nanoTime();
time = (stop - start) / 1000000L;
System.out.println("Rekursiv runtime: " + time + " ms");
count = 0;
start = System.nanoTime();
FibonacciParallel p = new FibonacciParallel();
long[] pResult = new long[NUMBERS.length];
for (int i : NUMBERS) {
pResult[count] = p.calculate(i);
count++;
}
stop = System.nanoTime();
time = (stop - start) / 1000000L;
System.out.println("Parallel runtime: " + time + " ms");
count = 0;
start = System.nanoTime();
FibonacciDynamic d = new FibonacciDynamic();
long[] dResult = new long[NUMBERS.length];
for (int i : NUMBERS) {
dResult[count] = d.calculate(i);
count++;
}
stop = System.nanoTime();
time = (stop - start) / 1000000L;
System.out.println("Dynamic runtime: " + time + " ms");
count = 0;
start = System.nanoTime();
FibonacciDynamicParallel dp = new FibonacciDynamicParallel();
long[] dpResult = new long[NUMBERS.length];
for (int i : NUMBERS) {
dpResult[count] = dp.calculate(i);
count++;
}
stop = System.nanoTime();
time = (stop - start) / 1000000L;
System.out.println("Dynamic Parallel runtime: " + time + " ms");
for (int i = 0; i < NUMBERS.length; i++) {
assertEquals(rResult[i], pResult[i]);
assertEquals(rResult[i], dResult[i]);
assertEquals(rResult[i], dpResult[i]);
assertEquals(pResult[i], dResult[i]);
assertEquals(pResult[i], dpResult[i]);
assertEquals(dResult[i], dpResult[i]);
}
}
}

@ -0,0 +1,7 @@
package provided._12._1;
public abstract class Fibonacci {
public abstract long calculate(int n);
}
Loading…
Cancel
Save