loophelpers.java
来自「SRI international 发布的OAA框架软件」· Java 代码 · 共 115 行
JAVA
115 行
/**
* Misc utilities in JSR166 performance tests
*/
import edu.emory.mathcs.backport.java.util.concurrent.*;
import edu.emory.mathcs.backport.java.util.concurrent.atomic.*;
import edu.emory.mathcs.backport.java.util.concurrent.helpers.*;
class LoopHelpers {
static final SimpleRandom staticRNG = new SimpleRandom();
// Some mindless computation to do between synchronizations...
/**
* generates 32 bit pseudo-random numbers.
* Adapted from http://www.snippets.org
*/
public static int compute1(int x) {
int lo = 16807 * (x & 0xFFFF);
int hi = 16807 * (x >>> 16);
lo += (hi & 0x7FFF) << 16;
if ((lo & 0x80000000) != 0) {
lo &= 0x7fffffff;
++lo;
}
lo += hi >>> 15;
if (lo == 0 || (lo & 0x80000000) != 0) {
lo &= 0x7fffffff;
++lo;
}
return lo;
}
/**
* Computes a linear congruential random number a random number
* of times.
*/
public static int compute2(int x) {
int loops = (x >>> 4) & 7;
while (loops-- > 0) {
x = (x * 2147483647) % 16807;
}
return x;
}
/**
* Yet another random number generator
*/
public static int compute3(int x) {
int t = (x % 127773) * 16807 - (x / 127773) * 2836;
return (t > 0)? t : t + 0x7fffffff;
}
/**
* Yet another random number generator
*/
public static int compute4(int x) {
return x * 134775813 + 1;
}
/**
* An actually useful random number generator, but unsynchronized.
* Basically same as java.util.Random.
*/
public static class SimpleRandom {
private final static long multiplier = 0x5DEECE66DL;
private final static long addend = 0xBL;
private final static long mask = (1L << 48) - 1;
static final AtomicLong seq = new AtomicLong(1);
private long seed = Utils.nanoTime() + seq.getAndIncrement();
public void setSeed(long s) {
seed = s;
}
public int next() {
long nextseed = (seed * multiplier + addend) & mask;
seed = nextseed;
return ((int)(nextseed >>> 17)) & 0x7FFFFFFF;
}
}
public static class BarrierTimer implements Runnable {
public volatile long startTime;
public volatile long endTime;
public void run() {
long t = Utils.nanoTime();
if (startTime == 0)
startTime = t;
else
endTime = t;
}
public void clear() {
startTime = 0;
endTime = 0;
}
public long getTime() {
return endTime - startTime;
}
}
public static String rightJustify(long n) {
// There's probably a better way to do this...
String field = " ";
String num = Long.toString(n);
if (num.length() >= field.length())
return num;
StringBuffer b = new StringBuffer(field);
b.replace(b.length()-num.length(), b.length(), num);
return b.toString();
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?