ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/loops/LoopHelpers.java
Revision: 1.1
Committed: Fri Apr 9 20:12:06 2004 UTC (20 years, 2 months ago) by jsr166
Branch: MAIN
Log Message:
Add ALoops benchmark to src/loops, target 'loops' already in build.xml

File Contents

# User Rev Content
1 jsr166 1.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     public static int compute3(int x) {
47     int t = (x % 127773) * 16807 - (x / 127773) * 2836;
48     return (t > 0)? t : t + 0x7fffffff;
49     }
50    
51     /**
52     * An actually useful random number generator, but unsynchronized.
53     * Basically same as java.util.Random.
54     */
55     public static class SimpleRandom {
56     private final static long multiplier = 0x5DEECE66DL;
57     private final static long addend = 0xBL;
58     private final static long mask = (1L << 48) - 1;
59     static final AtomicLong seq = new AtomicLong(1);
60     private long seed = System.nanoTime() + seq.getAndIncrement();
61    
62     public void setSeed(long s) {
63     seed = s;
64     }
65    
66     public int next() {
67     long nextseed = (seed * multiplier + addend) & mask;
68     seed = nextseed;
69     return ((int)(nextseed >>> 17)) & 0x7FFFFFFF;
70     }
71     }
72    
73     public static class BarrierTimer implements Runnable {
74     public volatile long startTime;
75     public volatile long endTime;
76     public void run() {
77     long t = System.nanoTime();
78     if (startTime == 0)
79     startTime = t;
80     else
81     endTime = t;
82     }
83     public void clear() {
84     startTime = 0;
85     endTime = 0;
86     }
87     public long getTime() {
88     return endTime - startTime;
89     }
90     }
91    
92     public static String rightJustify(long n) {
93     // There's probably a better way to do this...
94     String field = " ";
95     String num = Long.toString(n);
96     if (num.length() >= field.length())
97     return num;
98     StringBuffer b = new StringBuffer(field);
99     b.replace(b.length()-num.length(), b.length(), num);
100     return b.toString();
101     }
102    
103     }