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 |
/** |
8 |
* Misc utilities in JSR166 performance tests |
9 |
*/ |
10 |
class LoopHelpers { |
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 |
public static class BarrierTimer implements Runnable { |
47 |
public volatile long startTime; |
48 |
public volatile long endTime; |
49 |
public void run() { |
50 |
long t = System.nanoTime(); |
51 |
if (startTime == 0) |
52 |
startTime = t; |
53 |
else |
54 |
endTime = t; |
55 |
} |
56 |
public void clear() { |
57 |
startTime = 0; |
58 |
endTime = 0; |
59 |
} |
60 |
public long getTime() { |
61 |
return endTime - startTime; |
62 |
} |
63 |
} |
64 |
|
65 |
public static String rightJustify(long n) { |
66 |
// There's probably a better way to do this... |
67 |
String field = " "; |
68 |
String num = Long.toString(n); |
69 |
if (num.length() >= field.length()) |
70 |
return num; |
71 |
StringBuilder b = new StringBuilder(field); |
72 |
b.replace(b.length()-num.length(), b.length(), num); |
73 |
return b.toString(); |
74 |
} |
75 |
|
76 |
} |