1 |
/* |
2 |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
* Expert Group and released to the public domain, as explained at |
4 |
* http://creativecommons.org/licenses/publicdomain |
5 |
*/ |
6 |
|
7 |
import jsr166y.forkjoin.*; |
8 |
import java.util.*; |
9 |
|
10 |
class ScalarLongSortDemo { |
11 |
static final long NPS = (1000L * 1000 * 1000); |
12 |
|
13 |
public static void main (String[] args) throws Exception { |
14 |
int n = 1 << 20; |
15 |
int sreps = 4; |
16 |
int reps = 10; |
17 |
System.out.printf("Sorting %d longs, %d replications\n", n, sreps); |
18 |
long[] a = new long[n]; |
19 |
ForkJoinPool fjpool = new ForkJoinPool(); |
20 |
ParallelLongArray pa = ParallelLongArray.createUsingHandoff(a, fjpool); |
21 |
long max = 1234567890123L; |
22 |
|
23 |
for (int i = 0; i < sreps; ++i) { |
24 |
pa.replaceWithGeneratedValue(CommonOps.longRandom(max++)); |
25 |
long last = System.nanoTime(); |
26 |
java.util.Arrays.sort(a); |
27 |
double elapsed = (double)(System.nanoTime() - last) / NPS; |
28 |
System.out.printf("java.util.Arrays.sort time: %7.3f\n", elapsed); |
29 |
if (i == 0) |
30 |
checkSorted(a); |
31 |
} |
32 |
|
33 |
System.out.printf("Sorting %d longs, %d replications\n", n, reps); |
34 |
for (int i = 0; i < reps; ++i) { |
35 |
pa.replaceWithGeneratedValue(CommonOps.longRandom(max++)); |
36 |
long last = System.nanoTime(); |
37 |
pa.sort(); |
38 |
double elapsed = (double)(System.nanoTime() - last) / NPS; |
39 |
System.out.printf("ParallelLongArray.sort time: %7.3f\n", elapsed); |
40 |
if (i == 0) |
41 |
checkSorted(a); |
42 |
} |
43 |
|
44 |
fjpool.shutdown(); |
45 |
} |
46 |
|
47 |
static void checkSorted (long[] a) { |
48 |
int n = a.length; |
49 |
for (int i = 0; i < n - 1; i++) { |
50 |
if (a[i] > a[i+1]) { |
51 |
throw new Error("Unsorted at " + i + ": " + a[i] + " / " + a[i+1]); |
52 |
} |
53 |
} |
54 |
} |
55 |
|
56 |
|
57 |
} |