forked from adamldavis/hellojava8
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sort.java
70 lines (59 loc) · 1.97 KB
/
Sort.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import static java.lang.System.out;
import java.util.Arrays;
import java.util.Random;
import java.util.stream.Stream;
/**
* Tests whether filtering before or after sorting makes a difference.
* Spoiler-alert: it does.
*
* @author adamd
*/
public class Sort {
public static long time(Runnable r) {
long start = System.currentTimeMillis();
r.run();
return System.currentTimeMillis() - start;
}
// number of random numbers
public static final int n = 123456;
// just here to give algorithm something to modify...
public static int sum = 0;
public static long[] count = new long[3];
static final Random rnd = new Random();
public static void noFilter() {
Stream.generate(() -> rnd.nextInt()).limit(n).sorted((x, y) -> {
count[0]++;
return x > y ? 1 : -1;
}).forEach(x -> sum += x);
}
public static void filteredFirst() {
Stream.generate(() -> rnd.nextInt()).limit(n).filter(x -> x > 0)
.sorted((x, y) -> {
count[1]++;
return x > y ? 1 : -1;
}).forEach(x -> sum += x);
}
public static void filteredSecond() {
Stream.generate(() -> rnd.nextInt()).limit(n).sorted((x, y) -> {
count[2]++;
return x > y ? 1 : -1;
}).filter(x -> x > 0).forEach(x -> sum += x);
}
public static void main(String... args) {
Arrays.fill(count, 0);
long t1 = time(Sort::noFilter);
long t2 = time(Sort::filteredFirst);
long t3 = time(Sort::filteredSecond);
out.println("sum=" + sum); // meaningless
out.println("t1=" + t1 + " count=" + count[0]);
out.println("t2=" + t2 + " count=" + count[1]);
out.println("t3=" + t3 + " count=" + count[2]);
out.println("t1-t2 = " + ((double) (t1 - t2) / 1000.0) + " sec");
out.println("t1-t3 = " + ((double) (t1 - t3) / 1000.0) + " sec");
// time saved is probably negative or zero
out.println("count diffs =");
out.println("noFilter - filter1st: " + (count[0] - count[1]));
out.println("noFilter - filter2nd: " + (count[0] - count[2]));
// if the diff is small then sorting happened before the filtering.
}
}