ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/LoopHelpers.java
Revision: 1.2
Committed: Mon May 9 19:33:30 2005 UTC (19 years ago) by dl
Branch: MAIN
Changes since 1.1: +5 -0 lines
Log Message:
Add headers

File Contents

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