Adds up a big array of 1s with one worker, then splits the array evenly across several workers running at the same time. More processor cores should mean less time — up to a point.
| Workers | Time | Speed-up | Sum |
|---|
/**
* Created by mihailod, Jun 11, 2007, 8:50:56 PM
*/
public class Adder implements Runnable {
private int start;
private int end;
public double localSum = 0;
public Adder(int start, int end) {
this.start = start;
this.end = end;
}
public void run() {
for(int i=start; i<end; i++) {
localSum += ParallelSum.A[i];
}
}
}
/**
* Created by mihailod, Jun 11, 2007, 8:42:00 PM
*/
public class ParallelSum {
private static final int N = 1000000000;
public static double[] A = new double[N];
public static void main(String[] args) {
init();
long start = System.currentTimeMillis();
double sum = 0;
for(int i=0; i<N; i++) {
sum += A[i];
}
long time = System.currentTimeMillis() - start;
System.out.println("SERIAL sum = " + sum + " in " + time + "ms");
System.out.println("--------------------------");
start = System.currentTimeMillis();
final Adder a1 = new Adder(0, N/2);
final Adder a2 = new Adder(N/2, N);
final Thread t1 = new Thread(a1);
final Thread t2 = new Thread(a2);
t1.start();
t2.start();
try {
t1.join();
t2.join();
} catch(InterruptedException ie) {
ie.printStackTrace();
}
time = System.currentTimeMillis() - start;
System.out.println("PARALLEL sum = " + sum + " in " + time + "ms");
}
private static void init() {
for(int i=0; i<N; i++) {
A[i] = 1;
}
}
}