ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/extra166y/FindAnyDemo.java
Revision: 1.1
Committed: Sun Nov 1 22:00:35 2009 UTC (14 years, 7 months ago) by dl
Branch: MAIN
Log Message:
Move tests for extra166y

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.*;
8 import extra166y.*;
9 import java.util.*;
10 import java.util.concurrent.*;
11
12
13 public class FindAnyDemo {
14 static final int NCPU = Runtime.getRuntime().availableProcessors();
15
16 /**
17 * Sequential version, for performance comparison
18 */
19 static<T> int seqIndexOf(T[] array,
20 Ops.Predicate<T> pred) {
21 int n = array.length;
22 for (int i = 0; i < n; ++i) {
23 T x = array[i];
24 if (pred.op(x))
25 return i;
26 }
27 return -1;
28 }
29
30 /**
31 * Slow/dumb prime check
32 */
33 static class IsPrime implements Ops.Predicate<Rand> {
34 public boolean op(Rand r) {
35 long n = r.seed;
36 int bound = (int)(Math.sqrt(n));
37 if (bound >= 3) {
38 for (int i = 3; i <= bound; ++i)
39 if ((n & 1) == 0 || n % i == 0)
40 return false;
41 }
42 return true;
43 }
44 }
45
46 /** for time conversion */
47 static final long NPS = (1000L * 1000 * 1000);
48
49 static class NextRand implements Ops.Procedure<Rand> {
50 public void op(Rand r) {
51 r.next();
52 }
53 }
54
55 public static void main(String[] args) throws Exception {
56 int n = 1 << 20;
57 ArrayList<Rand> list = new ArrayList<Rand>(n);
58 long rs = 256203225;
59 for (int i = 0; i < n >>> 1; ++i)
60 list.add(new Rand(rs+=3));
61 list.add(new Rand(256203221));
62 for (int i = n >>> 1; i < n >>> 1; ++i)
63 list.add(new Rand(rs+=3));
64 Rand[] array = list.toArray(new Rand[0]);
65 final IsPrime pred = new IsPrime();
66 long last, now;
67 double elapsed;
68 boolean present = false;
69 for (int k = 0; k < 2; ++k) {
70 last = System.nanoTime();
71 for (int reps = 0; reps < 9; ++reps) {
72 int result = seqIndexOf(array, pred);
73 if (k == 0 && reps == 0)
74 present = result != -1;
75 else if (present != (result != -1))
76 throw new Error("Inconsistent result");
77 }
78 now = System.nanoTime();
79 elapsed = (double)(now - last) / NPS;
80 last = now;
81 System.out.printf("seq: %7.3f\n", elapsed);
82 }
83 Thread.sleep(100);
84 ForkJoinPool fjp = new ForkJoinPool(1);
85 ParallelArray<Rand> pa = ParallelArray.createUsingHandoff(array, fjp);
86 for (int i = 1; i <= NCPU; i <<= 1) {
87 fjp.setParallelism(i);
88 last = System.nanoTime();
89 for (int k = 0; k < 2; ++k) {
90 for (int reps = 0; reps < 9; ++reps) {
91 int result = pa.withFilter(pred).anyIndex();
92 if (present != (result != -1))
93 throw new Error("Inconsistent result");
94 }
95 now = System.nanoTime();
96 elapsed = (double)(now - last) / NPS;
97 last = now;
98 System.out.printf("ps %2d: %7.3f\n", i, elapsed);
99 }
100 }
101 fjp.shutdownNow();
102 fjp.awaitTermination(1, TimeUnit.SECONDS);
103 }
104
105 /**
106 * Unsynchronized version of java.util.Random algorithm.
107 */
108 static final class Rand {
109 private final static long multiplier = 0x5DEECE66DL;
110 private final static long addend = 0xBL;
111 private final static long mask = (1L << 48) - 1;
112 private long seed;
113
114 Rand(long s) {
115 seed = s;
116 // next();
117 // next();
118 }
119 public int next() {
120 long nextseed = (seed * multiplier + addend) & mask;
121 seed = nextseed;
122 return ((int)(nextseed >>> 17)) & 0x7FFFFFFF;
123 }
124
125 public String toString() {
126 return String.valueOf(seed);
127 }
128 }
129
130 }