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