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