ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/extra166y/ParallelArraySortDemo.java
Revision: 1.5
Committed: Tue Mar 15 19:47:04 2011 UTC (13 years, 1 month ago) by jsr166
Branch: MAIN
CVS Tags: release-1_7_0, HEAD
Changes since 1.4: +1 -1 lines
Log Message:
Update Creative Commons license URL in legal notices

File Contents

# User Rev Content
1 dl 1.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 jsr166 1.5 * http://creativecommons.org/publicdomain/zero/1.0/
5 dl 1.1 */
6    
7     import jsr166y.*;
8     import extra166y.*;
9     import java.util.*;
10    
11     class ParallelArraySortDemo {
12    
13     static final Random rng = new Random();
14     static final long NPS = (1000L * 1000 * 1000);
15    
16 jsr166 1.3 public static void main(String[] args) throws Exception {
17 dl 1.1 int n = 1 << 20;
18     int reps = 9;
19     System.out.printf("Sorting %d Longs, %d replications\n", n, reps);
20     Long[] a = new Long[n];
21     randomFill(a);
22    
23     for (int i = 0; i < reps; ++i) {
24     long last = System.nanoTime();
25     java.util.Arrays.sort(a);
26     double elapsed = (double)(System.nanoTime() - last) / NPS;
27     System.out.printf("java.util.Arrays.sort time: %7.3f\n", elapsed);
28     checkSorted(a);
29     shuffle(a);
30     // System.gc();
31     }
32    
33     ForkJoinPool fjpool = new ForkJoinPool();
34     ParallelArray<Long> pa = ParallelArray.createUsingHandoff(a, fjpool);
35     for (int i = 0; i < reps; ++i) {
36     long last = System.nanoTime();
37     pa.sort();
38     double elapsed = (double)(System.nanoTime() - last) / NPS;
39     System.out.printf("ArrayTasks.sort time: %7.3f\n", elapsed);
40     checkSorted(a);
41     shuffle(a);
42     // System.gc();
43     }
44     fjpool.shutdown();
45     }
46    
47 jsr166 1.4 static void checkSorted(Long[] a) {
48 dl 1.1 int n = a.length;
49     for (int i = 0; i < n - 1; i++) {
50     if (a[i].compareTo(a[i+1]) > 0) {
51     throw new Error("Unsorted at " + i + ": " + a[i] + " / " + a[i+1]);
52     }
53     }
54     }
55 jsr166 1.2
56 dl 1.1 static void randomFill(Long[] a) {
57     for (int i = 0; i < a.length; ++i)
58     a[i] = new Long(rng.nextLong());
59     }
60 jsr166 1.2
61 dl 1.1 static void shuffle(Long[] a) {
62     int n = a.length;
63     for (int i = n; i > 1; --i) {
64     int r = rng.nextInt(i);
65     Long t = a[i-1];
66     a[i-1] = a[r];
67     a[r] = t;
68     }
69     }
70    
71    
72     }