1 |
jsr166 |
1.7 |
/* |
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 |
jsr166 |
1.6 |
import java.util.concurrent.*; |
8 |
|
|
import java.util.concurrent.atomic.*; |
9 |
|
|
|
10 |
jsr166 |
1.1 |
/** |
11 |
|
|
* Misc utilities in JSR166 performance tests |
12 |
|
|
*/ |
13 |
|
|
class LoopHelpers { |
14 |
|
|
|
15 |
|
|
static final SimpleRandom staticRNG = new SimpleRandom(); |
16 |
|
|
|
17 |
|
|
// Some mindless computation to do between synchronizations... |
18 |
|
|
|
19 |
|
|
/** |
20 |
peierls |
1.2 |
* generates 32 bit pseudo-random numbers. |
21 |
jsr166 |
1.1 |
* Adapted from http://www.snippets.org |
22 |
|
|
*/ |
23 |
peierls |
1.2 |
public static int compute1(int x) { |
24 |
jsr166 |
1.1 |
int lo = 16807 * (x & 0xFFFF); |
25 |
|
|
int hi = 16807 * (x >>> 16); |
26 |
|
|
lo += (hi & 0x7FFF) << 16; |
27 |
|
|
if ((lo & 0x80000000) != 0) { |
28 |
|
|
lo &= 0x7fffffff; |
29 |
|
|
++lo; |
30 |
|
|
} |
31 |
|
|
lo += hi >>> 15; |
32 |
|
|
if (lo == 0 || (lo & 0x80000000) != 0) { |
33 |
|
|
lo &= 0x7fffffff; |
34 |
|
|
++lo; |
35 |
|
|
} |
36 |
|
|
return lo; |
37 |
|
|
} |
38 |
|
|
|
39 |
|
|
/** |
40 |
jsr166 |
1.5 |
* Computes a linear congruential random number a random number |
41 |
|
|
* of times. |
42 |
jsr166 |
1.1 |
*/ |
43 |
peierls |
1.2 |
public static int compute2(int x) { |
44 |
jsr166 |
1.1 |
int loops = (x >>> 4) & 7; |
45 |
|
|
while (loops-- > 0) { |
46 |
|
|
x = (x * 2147483647) % 16807; |
47 |
|
|
} |
48 |
|
|
return x; |
49 |
|
|
} |
50 |
|
|
|
51 |
peierls |
1.2 |
/** |
52 |
|
|
* Yet another random number generator |
53 |
|
|
*/ |
54 |
|
|
public static int compute3(int x) { |
55 |
jsr166 |
1.1 |
int t = (x % 127773) * 16807 - (x / 127773) * 2836; |
56 |
jsr166 |
1.3 |
return (t > 0) ? t : t + 0x7fffffff; |
57 |
jsr166 |
1.1 |
} |
58 |
|
|
|
59 |
|
|
/** |
60 |
peierls |
1.2 |
* Yet another random number generator |
61 |
|
|
*/ |
62 |
|
|
public static int compute4(int x) { |
63 |
|
|
return x * 134775813 + 1; |
64 |
|
|
} |
65 |
|
|
|
66 |
|
|
/** |
67 |
jsr166 |
1.1 |
* An actually useful random number generator, but unsynchronized. |
68 |
|
|
* Basically same as java.util.Random. |
69 |
|
|
*/ |
70 |
|
|
public static class SimpleRandom { |
71 |
jsr166 |
1.4 |
private static final long multiplier = 0x5DEECE66DL; |
72 |
|
|
private static final long addend = 0xBL; |
73 |
|
|
private static final long mask = (1L << 48) - 1; |
74 |
jsr166 |
1.1 |
static final AtomicLong seq = new AtomicLong(1); |
75 |
|
|
private long seed = System.nanoTime() + seq.getAndIncrement(); |
76 |
|
|
|
77 |
|
|
public void setSeed(long s) { |
78 |
|
|
seed = s; |
79 |
|
|
} |
80 |
|
|
|
81 |
|
|
public int next() { |
82 |
|
|
long nextseed = (seed * multiplier + addend) & mask; |
83 |
|
|
seed = nextseed; |
84 |
|
|
return ((int)(nextseed >>> 17)) & 0x7FFFFFFF; |
85 |
|
|
} |
86 |
|
|
} |
87 |
|
|
|
88 |
|
|
public static class BarrierTimer implements Runnable { |
89 |
|
|
public volatile long startTime; |
90 |
|
|
public volatile long endTime; |
91 |
|
|
public void run() { |
92 |
|
|
long t = System.nanoTime(); |
93 |
|
|
if (startTime == 0) |
94 |
|
|
startTime = t; |
95 |
|
|
else |
96 |
|
|
endTime = t; |
97 |
|
|
} |
98 |
|
|
public void clear() { |
99 |
|
|
startTime = 0; |
100 |
|
|
endTime = 0; |
101 |
|
|
} |
102 |
|
|
public long getTime() { |
103 |
|
|
return endTime - startTime; |
104 |
|
|
} |
105 |
|
|
} |
106 |
|
|
|
107 |
|
|
public static String rightJustify(long n) { |
108 |
|
|
// There's probably a better way to do this... |
109 |
|
|
String field = " "; |
110 |
|
|
String num = Long.toString(n); |
111 |
|
|
if (num.length() >= field.length()) |
112 |
|
|
return num; |
113 |
|
|
StringBuffer b = new StringBuffer(field); |
114 |
|
|
b.replace(b.length()-num.length(), b.length(), num); |
115 |
|
|
return b.toString(); |
116 |
|
|
} |
117 |
peierls |
1.2 |
|
118 |
jsr166 |
1.1 |
} |