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/publicdomain/zero/1.0/ |
5 |
*/ |
6 |
|
7 |
import java.util.concurrent.*; |
8 |
import java.util.concurrent.atomic.*; |
9 |
|
10 |
/** |
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 |
* generates 32 bit pseudo-random numbers. |
21 |
* Adapted from http://www.snippets.org |
22 |
*/ |
23 |
public static int compute1(int x) { |
24 |
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 |
* Computes a linear congruential random number a random number |
41 |
* of times. |
42 |
*/ |
43 |
public static int compute2(int x) { |
44 |
int loops = (x >>> 4) & 7; |
45 |
while (loops-- > 0) { |
46 |
x = (x * 2147483647) % 16807; |
47 |
} |
48 |
return x; |
49 |
} |
50 |
|
51 |
/** |
52 |
* Yet another random number generator |
53 |
*/ |
54 |
public static int compute3(int x) { |
55 |
int t = (x % 127773) * 16807 - (x / 127773) * 2836; |
56 |
return (t > 0) ? t : t + 0x7fffffff; |
57 |
} |
58 |
|
59 |
/** |
60 |
* Yet another random number generator |
61 |
*/ |
62 |
public static int compute4(int x) { |
63 |
return x * 134775813 + 1; |
64 |
} |
65 |
|
66 |
/** |
67 |
* An actually useful random number generator, but unsynchronized. |
68 |
* Basically same as java.util.Random. |
69 |
*/ |
70 |
public static class SimpleRandom { |
71 |
private static final long multiplier = 0x5DEECE66DL; |
72 |
private static final long addend = 0xBL; |
73 |
private static final long mask = (1L << 48) - 1; |
74 |
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 |
|
118 |
} |