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/publicdomain/zero/1.0/ |
5 |
*/ |
6 |
|
7 |
import jsr166y.*; |
8 |
import extra166y.*; |
9 |
import java.util.*; |
10 |
|
11 |
class ScalarLongSortDemo { |
12 |
static final long NPS = (1000L * 1000 * 1000); |
13 |
|
14 |
public static void main(String[] args) throws Exception { |
15 |
int n = 1 << 22; |
16 |
int sreps = 4; |
17 |
int reps = 10; |
18 |
System.out.printf("Sorting %d longs, %d replications\n", n, sreps); |
19 |
long[] a = new long[n]; |
20 |
ForkJoinPool fjpool = new ForkJoinPool(); |
21 |
ParallelLongArray pa = ParallelLongArray.createUsingHandoff(a, fjpool); |
22 |
long max = 1234567890123L; |
23 |
|
24 |
for (int i = 0; i < sreps; ++i) { |
25 |
pa.replaceWithGeneratedValue(CommonOps.longRandom(max++)); |
26 |
long last = System.nanoTime(); |
27 |
java.util.Arrays.sort(a); |
28 |
double elapsed = (double)(System.nanoTime() - last) / NPS; |
29 |
System.out.printf("java.util.Arrays.sort time: %7.3f\n", elapsed); |
30 |
if (i == 0) |
31 |
checkSorted(a); |
32 |
} |
33 |
|
34 |
System.out.printf("Sorting %d longs, %d replications\n", n, reps); |
35 |
for (int i = 0; i < reps; ++i) { |
36 |
pa.replaceWithGeneratedValue(CommonOps.longRandom(max++)); |
37 |
long last = System.nanoTime(); |
38 |
pa.sort(); |
39 |
double elapsed = (double)(System.nanoTime() - last) / NPS; |
40 |
System.out.printf("ParallelLongArray.sort time: %7.3f\n", elapsed); |
41 |
if (i == 0) |
42 |
checkSorted(a); |
43 |
} |
44 |
|
45 |
fjpool.shutdown(); |
46 |
} |
47 |
|
48 |
static void checkSorted(long[] a) { |
49 |
int n = a.length; |
50 |
for (int i = 0; i < n - 1; i++) { |
51 |
if (a[i] > a[i+1]) { |
52 |
throw new Error("Unsorted at " + i + ": " + a[i] + " / " + a[i+1]); |
53 |
} |
54 |
} |
55 |
} |
56 |
|
57 |
|
58 |
} |