Skip to content

Instantly share code, notes, and snippets.

@szarnekow
Created October 28, 2013 08:07
Show Gist options
  • Save szarnekow/7193025 to your computer and use it in GitHub Desktop.
Save szarnekow/7193025 to your computer and use it in GitHub Desktop.
import java.util.Arrays;
import java.util.stream.IntStream;
public class C {
public int parallelSumJava8(int reps) {
int result = 0;
for (int i = 0; i < reps; i++) {
final int i_ = i;
result += Arrays.stream(array).parallel().map(e -> e * 5 * i_).sum();
}
return result;
}
public int sum(int reps) {
int result = 0;
for (int i = 0; i < reps; i++) {
int j = 0;
for(int k : array) {
j += k * 5 * i;
}
result += j;
}
return result;
}
public int sequentialSumJava8(int reps) {
int result = 0;
for (int i = 0; i < reps; i++) {
final int i_ = i;
result += Arrays.stream(array).map(e -> e * 5 * i_).sum();
}
return result;
}
public void m(int reps) {
long time = System.currentTimeMillis();
int expectation = sum(reps);
long took = System.currentTimeMillis() - time;
System.out.println("sum took " + took + " ms");
time = System.currentTimeMillis();
if (expectation != parallelSumJava8(reps)) {
throw new IllegalStateException();
}
took = System.currentTimeMillis() - time;
System.out.println("parallelSumJava8 took " + took + " ms");
time = System.currentTimeMillis();
if (expectation != sequentialSumJava8(reps)) {
throw new IllegalStateException();
}
took = System.currentTimeMillis() - time;
System.out.println("sequentialSumJava8 took " + took + " ms");
}
C() {
this.length = 5000000;
setUp();
}
private int[] array;
private int length;
protected void setUp() {
array = new int[length];
for(int i = 0; i < length; i++) {
array[i] = 3 * i;
}
}
public static void main(String[] args) {
C c = new C();
while(true) {
c.m(100);
}
}
}
sum took 506 ms
parallelSumJava8 took 245 ms
sequentialSumJava8 took 896 ms
sum took 499 ms
parallelSumJava8 took 411 ms
sequentialSumJava8 took 875 ms
sum took 442 ms
parallelSumJava8 took 908 ms
sequentialSumJava8 took 819 ms
sum took 443 ms
parallelSumJava8 took 889 ms
sequentialSumJava8 took 898 ms
sum took 447 ms
parallelSumJava8 took 867 ms
sequentialSumJava8 took 814 ms
sum took 467 ms
parallelSumJava8 took 943 ms
sequentialSumJava8 took 818 ms
sum took 454 ms
parallelSumJava8 took 1456 ms
sequentialSumJava8 took 806 ms
sum took 437 ms
parallelSumJava8 took 983 ms
sequentialSumJava8 took 1106 ms
sum took 423 ms
parallelSumJava8 took 1046 ms
sequentialSumJava8 took 850 ms
sum took 413 ms
parallelSumJava8 took 1054 ms
sequentialSumJava8 took 830 ms
sum took 417 ms
parallelSumJava8 took 1181 ms
sequentialSumJava8 took 915 ms
sum took 425 ms
parallelSumJava8 took 1005 ms
sequentialSumJava8 took 868 ms
sum took 429 ms
parallelSumJava8 took 973 ms
sequentialSumJava8 took 892 ms
sum took 438 ms
parallelSumJava8 took 1025 ms
sequentialSumJava8 took 837 ms
sum took 436 ms
parallelSumJava8 took 982 ms
sequentialSumJava8 took 844 ms
sum took 471 ms
parallelSumJava8 took 1046 ms
sequentialSumJava8 took 876 ms
sum took 488 ms
parallelSumJava8 took 1086 ms
sequentialSumJava8 took 1008 ms
sum took 448 ms
parallelSumJava8 took 1010 ms
sequentialSumJava8 took 975 ms
sum took 480 ms
parallelSumJava8 took 1223 ms
sequentialSumJava8 took 1092 ms
sum took 441 ms
parallelSumJava8 took 1101 ms
sequentialSumJava8 took 941 ms
sum took 436 ms
parallelSumJava8 took 1079 ms
sequentialSumJava8 took 1018 ms
sum took 457 ms
parallelSumJava8 took 1302 ms
sequentialSumJava8 took 1006 ms
sum took 440 ms
parallelSumJava8 took 1243 ms
sequentialSumJava8 took 1031 ms
sum took 457 ms
parallelSumJava8 took 1178 ms
sequentialSumJava8 took 1004 ms
sum took 443 ms
parallelSumJava8 took 1080 ms
sequentialSumJava8 took 919 ms
sum took 462 ms
parallelSumJava8 took 1105 ms
sequentialSumJava8 took 914 ms
sum took 482 ms
parallelSumJava8 took 1047 ms
sequentialSumJava8 took 956 ms
sum took 462 ms
parallelSumJava8 took 1078 ms
sequentialSumJava8 took 931 ms
sum took 508 ms
parallelSumJava8 took 1275 ms
sequentialSumJava8 took 1081 ms
sum took 515 ms
parallelSumJava8 took 1223 ms
sequentialSumJava8 took 1065 ms
sum took 446 ms
parallelSumJava8 took 1197 ms
sequentialSumJava8 took 980 ms
sum took 507 ms
parallelSumJava8 took 1405 ms
sequentialSumJava8 took 1014 ms
sum took 441 ms
parallelSumJava8 took 1071 ms
sequentialSumJava8 took 1010 ms
sum took 469 ms
parallelSumJava8 took 1195 ms
sequentialSumJava8 took 3100 ms
sum took 485 ms
parallelSumJava8 took 1153 ms
sequentialSumJava8 took 3026 ms
sum took 526 ms
parallelSumJava8 took 1144 ms
sequentialSumJava8 took 3132 ms
sum took 536 ms
parallelSumJava8 took 1097 ms
sequentialSumJava8 took 2893 ms
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment