ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/jsr166y/SortDemo.java
Revision: 1.7
Committed: Sun Nov 1 21:50:49 2009 UTC (14 years, 7 months ago) by dl
Branch: MAIN
CVS Tags: HEAD
Changes since 1.6: +0 -0 lines
State: FILE REMOVED
Log Message:
move tests for unshipped classes

File Contents

# Content
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 SortDemo {
11
12 static final Random rng = new Random();
13 static final long NPS = (1000L * 1000 * 1000);
14
15 public static void main (String[] args) throws Exception {
16 int n = 1 << 20;
17 int sreps = 4;
18 int reps = 10;
19 Long[] a = new Long[n];
20 ForkJoinPool fjpool = new ForkJoinPool();
21 ParallelArray<Long> pa = ParallelArray.createUsingHandoff(a, fjpool);
22
23 System.out.printf("Sorting %d Longs, %d replications\n", n, sreps);
24 for (int i = 0; i < sreps; ++i) {
25 pa.replaceWithGeneratedValue(rlg);
26 seqSort(a);
27 if (i == 0)
28 checkSorted(a);
29 }
30
31 System.out.printf("Sorting %d Longs, %d replications\n", n, reps);
32 for (int i = 0; i < reps; ++i) {
33 pa.replaceWithGeneratedValue(rlg);
34 parSort(pa);
35 if (i == 0)
36 checkSorted(a);
37 }
38 fjpool.shutdown();
39 }
40
41 static void seqSort(Long[] a) {
42 long last = System.nanoTime();
43 java.util.Arrays.sort(a);
44 double elapsed = (double)(System.nanoTime() - last) / NPS;
45 System.out.printf("java.util.Arrays.sort time: %7.3f\n", elapsed);
46 }
47
48 static void parSort(ParallelArray<Long> pa) {
49 long last = System.nanoTime();
50 pa.sort();
51 double elapsed = (double)(System.nanoTime() - last) / NPS;
52 System.out.printf("ParallelArray.sort time: %7.3f\n", elapsed);
53 }
54
55 static void checkSorted (Long[] a) {
56 int n = a.length;
57 for (int i = 0; i < n - 1; i++) {
58 if (a[i].compareTo(a[i+1]) > 0) {
59 throw new Error("Unsorted at " + i + ": " + a[i] + " / " + a[i+1]);
60 }
61 }
62 }
63
64 static final class RandomLongGenerator implements Ops.Generator<Long> {
65 public Long op() {
66 return new Long(ForkJoinWorkerThread.nextRandomLong());
67 }
68 }
69
70 static final RandomLongGenerator rlg = new RandomLongGenerator();
71
72
73
74 }