ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/LoopHelpers.java
Revision: 1.1
Committed: Mon May 2 19:19:38 2005 UTC (19 years ago) by dl
Branch: MAIN
Log Message:
Put misc performance tests into CVS

File Contents

# Content
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 /**
63 * Yet another random number generator
64 */
65 public static int compute5(int x) {
66 return 36969 * (x & 65535) + (x >> 16);
67 }
68
69
70 /**
71 * An actually useful random number generator, but unsynchronized.
72 * Basically same as java.util.Random.
73 */
74 public static class SimpleRandom {
75 private final static long multiplier = 0x5DEECE66DL;
76 private final static long addend = 0xBL;
77 private final static long mask = (1L << 48) - 1;
78 static final AtomicLong seq = new AtomicLong(1);
79 private long seed = System.nanoTime() + seq.getAndIncrement();
80
81 public void setSeed(long s) {
82 seed = s;
83 }
84
85 public int next() {
86 long nextseed = (seed * multiplier + addend) & mask;
87 seed = nextseed;
88 return ((int)(nextseed >>> 17)) & 0x7FFFFFFF;
89 }
90 }
91
92 public static class BarrierTimer implements Runnable {
93 public volatile long startTime;
94 public volatile long endTime;
95 public void run() {
96 long t = System.nanoTime();
97 if (startTime == 0)
98 startTime = t;
99 else
100 endTime = t;
101 }
102 public void clear() {
103 startTime = 0;
104 endTime = 0;
105 }
106 public long getTime() {
107 return endTime - startTime;
108 }
109 }
110
111 public static String rightJustify(long n) {
112 // There's probably a better way to do this...
113 String field = " ";
114 String num = Long.toString(n);
115 if (num.length() >= field.length())
116 return num;
117 StringBuffer b = new StringBuffer(field);
118 b.replace(b.length()-num.length(), b.length(), num);
119 return b.toString();
120 }
121
122 }