You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ah...@apache.org on 2019/04/10 10:01:10 UTC
[commons-rng] 01/02: Update JMH benchmarks for the XorShiRo
generators.
This is an automated email from the ASF dual-hosted git repository.
aherbert pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-rng.git
commit 402c1e0e8bdbb1a01743f5b07cf39884fce4a8b0
Author: aherbert <ah...@apache.org>
AuthorDate: Wed Apr 10 10:56:02 2019 +0100
Update JMH benchmarks for the XorShiRo generators.
---
.../commons/rng/examples/jmh/BaselineSources.java | 42 ++--
.../rng/examples/jmh/ConstructionPerformance.java | 222 +++++++++++++++++----
.../rng/examples/jmh/GenerationPerformance.java | 63 +-----
...{RandomSources.java => RandomSourceValues.java} | 34 ++--
.../commons/rng/examples/jmh/RandomSources.java | 40 +---
.../distribution/GeometricSamplersPerformance.java | 2 +-
.../jmh/distribution/NextGaussianPerformance.java | 2 +-
.../PoissonSamplerCachePerformance.java | 2 +-
.../jmh/distribution/SamplersPerformance.java | 76 ++-----
9 files changed, 262 insertions(+), 221 deletions(-)
diff --git a/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/BaselineSources.java b/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/BaselineSources.java
index bb5c78e..1659fea 100644
--- a/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/BaselineSources.java
+++ b/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/BaselineSources.java
@@ -43,22 +43,32 @@ public abstract class BaselineSources {
* is in the declared order of {@link RandomSource}.</p>
*/
@Param({BASELINE,
- "JDK",
- "WELL_512_A",
- "WELL_1024_A",
- "WELL_19937_A",
- "WELL_19937_C",
- "WELL_44497_A",
- "WELL_44497_B",
- "MT",
- "ISAAC",
- "SPLIT_MIX_64",
- "XOR_SHIFT_1024_S",
- "TWO_CMRES",
- "MT_64",
- "MWC_256",
- "KISS",
- "XOR_SHIFT_1024_S_PHI",
+ "JDK",
+ "WELL_512_A",
+ "WELL_1024_A",
+ "WELL_19937_A",
+ "WELL_19937_C",
+ "WELL_44497_A",
+ "WELL_44497_B",
+ "MT",
+ "ISAAC",
+ "SPLIT_MIX_64",
+ "XOR_SHIFT_1024_S",
+ "TWO_CMRES",
+ "MT_64",
+ "MWC_256",
+ "KISS",
+ "XOR_SHIFT_1024_S_PHI",
+ "XO_RO_SHI_RO_64_S",
+ "XO_RO_SHI_RO_64_SS",
+ "XO_SHI_RO_128_PLUS",
+ "XO_SHI_RO_128_SS",
+ "XO_RO_SHI_RO_128_PLUS",
+ "XO_RO_SHI_RO_128_SS",
+ "XO_SHI_RO_256_PLUS",
+ "XO_SHI_RO_256_SS",
+ "XO_SHI_RO_512_PLUS",
+ "XO_SHI_RO_512_SS",
})
private String randomSourceName;
diff --git a/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/ConstructionPerformance.java b/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/ConstructionPerformance.java
index ba732e0..c560bcb 100644
--- a/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/ConstructionPerformance.java
+++ b/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/ConstructionPerformance.java
@@ -24,7 +24,6 @@ import org.openjdk.jmh.annotations.Level;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
-import org.openjdk.jmh.annotations.Param;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
@@ -47,10 +46,21 @@ import org.apache.commons.rng.core.source32.Well19937c;
import org.apache.commons.rng.core.source32.Well44497a;
import org.apache.commons.rng.core.source32.Well44497b;
import org.apache.commons.rng.core.source32.Well512a;
+import org.apache.commons.rng.core.source32.XoRoShiRo64Star;
+import org.apache.commons.rng.core.source32.XoRoShiRo64StarStar;
+import org.apache.commons.rng.core.source32.XoShiRo128Plus;
+import org.apache.commons.rng.core.source32.XoShiRo128StarStar;
import org.apache.commons.rng.core.source64.MersenneTwister64;
import org.apache.commons.rng.core.source64.SplitMix64;
import org.apache.commons.rng.core.source64.TwoCmres;
+import org.apache.commons.rng.core.source64.XoRoShiRo128Plus;
+import org.apache.commons.rng.core.source64.XoRoShiRo128StarStar;
+import org.apache.commons.rng.core.source64.XoShiRo256Plus;
+import org.apache.commons.rng.core.source64.XoShiRo256StarStar;
+import org.apache.commons.rng.core.source64.XoShiRo512Plus;
+import org.apache.commons.rng.core.source64.XoShiRo512StarStar;
import org.apache.commons.rng.core.source64.XorShift1024Star;
+import org.apache.commons.rng.core.source64.XorShift1024StarPhi;
import org.apache.commons.rng.core.util.NumberFactory;
import org.apache.commons.rng.simple.RandomSource;
import org.apache.commons.rng.simple.internal.ProviderBuilder.RandomSourceInternal;
@@ -103,7 +113,7 @@ public class ConstructionPerformance {
LONG_ARRAY_SEEDS = new long[SEEDS][];
INT_ARRAY_SEEDS = new int[SEEDS][];
BYTE_ARRAY_SEEDS = new byte[SEEDS][];
- final UniformRandomProvider rng = RandomSource.create(RandomSource.XOR_SHIFT_1024_S);
+ final UniformRandomProvider rng = RandomSource.create(RandomSource.XOR_SHIFT_1024_S_PHI);
for (int i = 0; i < SEEDS; i++) {
final long[] longArray = new long[MAX_SEED_SIZE];
final int[] intArray = new int[MAX_SEED_SIZE];
@@ -123,31 +133,7 @@ public class ConstructionPerformance {
* The benchmark state (retrieve the various "RandomSource"s).
*/
@State(Scope.Benchmark)
- public static class Sources {
- /**
- * RNG providers.
- */
- @Param({"JDK",
- "WELL_512_A",
- "WELL_1024_A",
- "WELL_19937_A",
- "WELL_19937_C",
- "WELL_44497_A",
- "WELL_44497_B",
- "MT",
- "ISAAC",
- "SPLIT_MIX_64",
- "XOR_SHIFT_1024_S",
- "TWO_CMRES",
- "MT_64",
- "MWC_256",
- "KISS",
- })
- private String randomSourceName;
-
- /** The RandomSource. */
- private RandomSource randomSource;
-
+ public static class Sources extends RandomSourceValues {
/** The native seeds. */
private Object[] nativeSeeds;
@@ -164,15 +150,6 @@ public class ConstructionPerformance {
private Constructor<Object> constructor;
/**
- * Gets the random source.
- *
- * @return the random source
- */
- public RandomSource getRandomSource() {
- return randomSource;
- }
-
- /**
* Gets the native seeds for the RandomSource.
*
* @return the native seeds
@@ -219,13 +196,13 @@ public class ConstructionPerformance {
/**
* Create the random source and the test seeds.
- *
- * @throws NoSuchMethodException If the constructor cannot be found
*/
+ @Override
@SuppressWarnings("unchecked")
@Setup(value=Level.Trial)
- public void setup() throws NoSuchMethodException {
- randomSource = RandomSource.valueOf(randomSourceName);
+ public void setup() {
+ super.setup();
+ RandomSource randomSource = getRandomSource();
nativeSeeds = findNativeSeeds(randomSource);
// Truncate array seeds to length 1
@@ -249,7 +226,11 @@ public class ConstructionPerformance {
// Cache the class type and constructor
implementingClass = getRandomSourceInternal(randomSource).getRng();
- constructor = (Constructor<Object>) implementingClass.getConstructor(nativeSeeds[0].getClass());
+ try {
+ constructor = (Constructor<Object>) implementingClass.getConstructor(nativeSeeds[0].getClass());
+ } catch (NoSuchMethodException ex) {
+ throw new IllegalStateException("Failed to find the constructor", ex);
+ }
}
/**
@@ -293,9 +274,20 @@ public class ConstructionPerformance {
case ISAAC:
case MWC_256:
case KISS:
+ case XO_RO_SHI_RO_64_S:
+ case XO_RO_SHI_RO_64_SS:
+ case XO_SHI_RO_128_PLUS:
+ case XO_SHI_RO_128_SS:
return INT_ARRAY_SEEDS;
case XOR_SHIFT_1024_S:
+ case XOR_SHIFT_1024_S_PHI:
case MT_64:
+ case XO_RO_SHI_RO_128_PLUS:
+ case XO_RO_SHI_RO_128_SS:
+ case XO_SHI_RO_256_PLUS:
+ case XO_SHI_RO_256_SS:
+ case XO_SHI_RO_512_PLUS:
+ case XO_SHI_RO_512_SS:
return LONG_ARRAY_SEEDS;
default:
throw new AssertionError("Unknown native seed");
@@ -330,6 +322,7 @@ public class ConstructionPerformance {
case ISAAC:
return 256;
case XOR_SHIFT_1024_S:
+ case XOR_SHIFT_1024_S_PHI:
return 16;
case MT_64:
return 312;
@@ -337,6 +330,21 @@ public class ConstructionPerformance {
return 257;
case KISS:
return 4;
+ case XO_RO_SHI_RO_64_S:
+ case XO_RO_SHI_RO_64_SS:
+ return 2;
+ case XO_SHI_RO_128_PLUS:
+ case XO_SHI_RO_128_SS:
+ return 4;
+ case XO_RO_SHI_RO_128_PLUS:
+ case XO_RO_SHI_RO_128_SS:
+ return 2;
+ case XO_SHI_RO_256_PLUS:
+ case XO_SHI_RO_256_SS:
+ return 4;
+ case XO_SHI_RO_512_PLUS:
+ case XO_SHI_RO_512_SS:
+ return 8;
default:
throw new AssertionError("Unknown native seed size");
}
@@ -363,10 +371,21 @@ public class ConstructionPerformance {
case TWO_CMRES_SELECT:
case MWC_256:
case KISS:
+ case XO_RO_SHI_RO_64_S:
+ case XO_RO_SHI_RO_64_SS:
+ case XO_SHI_RO_128_PLUS:
+ case XO_SHI_RO_128_SS:
return 4; // int
case SPLIT_MIX_64:
case XOR_SHIFT_1024_S:
+ case XOR_SHIFT_1024_S_PHI:
case MT_64:
+ case XO_RO_SHI_RO_128_PLUS:
+ case XO_RO_SHI_RO_128_SS:
+ case XO_SHI_RO_256_PLUS:
+ case XO_SHI_RO_256_SS:
+ case XO_SHI_RO_512_PLUS:
+ case XO_SHI_RO_512_SS:
return 8; // long
default:
throw new AssertionError("Unknown native seed element byte size");
@@ -397,6 +416,17 @@ public class ConstructionPerformance {
case SPLIT_MIX_64: return RandomSourceInternal.SPLIT_MIX_64;
case XOR_SHIFT_1024_S: return RandomSourceInternal.XOR_SHIFT_1024_S;
case MT_64: return RandomSourceInternal.MT_64;
+ case XOR_SHIFT_1024_S_PHI: return RandomSourceInternal.XOR_SHIFT_1024_S_PHI;
+ case XO_RO_SHI_RO_64_S: return RandomSourceInternal.XO_RO_SHI_RO_64_S;
+ case XO_RO_SHI_RO_64_SS: return RandomSourceInternal.XO_RO_SHI_RO_64_SS;
+ case XO_SHI_RO_128_PLUS: return RandomSourceInternal.XO_SHI_RO_128_PLUS;
+ case XO_SHI_RO_128_SS: return RandomSourceInternal.XO_SHI_RO_128_SS;
+ case XO_RO_SHI_RO_128_PLUS: return RandomSourceInternal.XO_RO_SHI_RO_128_PLUS;
+ case XO_RO_SHI_RO_128_SS: return RandomSourceInternal.XO_RO_SHI_RO_128_SS;
+ case XO_SHI_RO_256_PLUS: return RandomSourceInternal.XO_SHI_RO_256_PLUS;
+ case XO_SHI_RO_256_SS: return RandomSourceInternal.XO_SHI_RO_256_SS;
+ case XO_SHI_RO_512_PLUS: return RandomSourceInternal.XO_SHI_RO_512_PLUS;
+ case XO_SHI_RO_512_SS: return RandomSourceInternal.XO_SHI_RO_512_SS;
default:
throw new AssertionError("Unknown random source internal");
}
@@ -554,6 +584,116 @@ public class ConstructionPerformance {
}
/**
+ * @param bh Data sink.
+ */
+ @Benchmark
+ public void newXorShift1024StarPhi(Blackhole bh) {
+ for (int i = 0; i < SEEDS; i++) {
+ bh.consume(new XorShift1024StarPhi(LONG_ARRAY_SEEDS[i]));
+ }
+ }
+
+ /**
+ * @param bh Data sink.
+ */
+ @Benchmark
+ public void newXoRoShiRo64Star(Blackhole bh) {
+ for (int i = 0; i < SEEDS; i++) {
+ bh.consume(new XoRoShiRo64Star(INT_ARRAY_SEEDS[i]));
+ }
+ }
+
+ /**
+ * @param bh Data sink.
+ */
+ @Benchmark
+ public void newXoRoShiRo64StarStar(Blackhole bh) {
+ for (int i = 0; i < SEEDS; i++) {
+ bh.consume(new XoRoShiRo64StarStar(INT_ARRAY_SEEDS[i]));
+ }
+ }
+
+ /**
+ * @param bh Data sink.
+ */
+ @Benchmark
+ public void newXoShiRo128Plus(Blackhole bh) {
+ for (int i = 0; i < SEEDS; i++) {
+ bh.consume(new XoShiRo128Plus(INT_ARRAY_SEEDS[i]));
+ }
+ }
+
+ /**
+ * @param bh Data sink.
+ */
+ @Benchmark
+ public void newXoShiRo128StarStar(Blackhole bh) {
+ for (int i = 0; i < SEEDS; i++) {
+ bh.consume(new XoShiRo128StarStar(INT_ARRAY_SEEDS[i]));
+ }
+ }
+
+ /**
+ * @param bh Data sink.
+ */
+ @Benchmark
+ public void newXoRoShiRo128Plus(Blackhole bh) {
+ for (int i = 0; i < SEEDS; i++) {
+ bh.consume(new XoRoShiRo128Plus(LONG_ARRAY_SEEDS[i]));
+ }
+ }
+
+ /**
+ * @param bh Data sink.
+ */
+ @Benchmark
+ public void newXoRoShiRo128StarStar(Blackhole bh) {
+ for (int i = 0; i < SEEDS; i++) {
+ bh.consume(new XoRoShiRo128StarStar(LONG_ARRAY_SEEDS[i]));
+ }
+ }
+
+ /**
+ * @param bh Data sink.
+ */
+ @Benchmark
+ public void newXoShiRo256Plus(Blackhole bh) {
+ for (int i = 0; i < SEEDS; i++) {
+ bh.consume(new XoShiRo256Plus(LONG_ARRAY_SEEDS[i]));
+ }
+ }
+
+ /**
+ * @param bh Data sink.
+ */
+ @Benchmark
+ public void newXoShiRo256StarStar(Blackhole bh) {
+ for (int i = 0; i < SEEDS; i++) {
+ bh.consume(new XoShiRo256StarStar(LONG_ARRAY_SEEDS[i]));
+ }
+ }
+
+ /**
+ * @param bh Data sink.
+ */
+ @Benchmark
+ public void newXoShiRo512Plus(Blackhole bh) {
+ for (int i = 0; i < SEEDS; i++) {
+ bh.consume(new XoShiRo512Plus(LONG_ARRAY_SEEDS[i]));
+ }
+ }
+
+ /**
+ * @param bh Data sink.
+ */
+ @Benchmark
+ public void newXoShiRo512StarStar(Blackhole bh) {
+ for (int i = 0; i < SEEDS; i++) {
+ bh.consume(new XoShiRo512StarStar(LONG_ARRAY_SEEDS[i]));
+ }
+ }
+
+ /**
* Create a new instance using reflection with a cached constructor.
*
* @param sources Source of randomness.
diff --git a/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/GenerationPerformance.java b/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/GenerationPerformance.java
index 82a82e8..6b4e993 100644
--- a/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/GenerationPerformance.java
+++ b/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/GenerationPerformance.java
@@ -26,14 +26,10 @@ import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Param;
-import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.infra.Blackhole;
import java.util.concurrent.TimeUnit;
-import org.apache.commons.rng.UniformRandomProvider;
-import org.apache.commons.rng.simple.RandomSource;
-
/**
* Executes benchmark to compare the speed of generation of random numbers
* from the various source providers.
@@ -46,49 +42,6 @@ import org.apache.commons.rng.simple.RandomSource;
@Fork(value = 1, jvmArgs = {"-server", "-Xms128M", "-Xmx128M"})
public class GenerationPerformance {
/**
- * The benchmark state (retrieve the various "RandomSource"s).
- */
- @State(Scope.Benchmark)
- public static class Sources {
- /**
- * RNG providers.
- */
- @Param({"JDK",
- "WELL_512_A",
- "WELL_1024_A",
- "WELL_19937_A",
- "WELL_19937_C",
- "WELL_44497_A",
- "WELL_44497_B",
- "MT",
- "ISAAC",
- "SPLIT_MIX_64",
- "MWC_256",
- "KISS",
- "XOR_SHIFT_1024_S",
- "TWO_CMRES",
- "MT_64" })
- private String randomSourceName;
-
- /** RNG. */
- private UniformRandomProvider provider;
-
- /**
- * @return the RNG.
- */
- public UniformRandomProvider getGenerator() {
- return provider;
- }
-
- /** Instantiates generator. */
- @Setup
- public void setup() {
- final RandomSource randomSource = RandomSource.valueOf(randomSourceName);
- provider = RandomSource.create(randomSource);
- }
- }
-
- /**
* Number of random values to generate.
*/
@Param({"1", "100", "10000", "1000000"})
@@ -99,7 +52,7 @@ public class GenerationPerformance {
* @param bh Data sink.
*/
@Benchmark
- public void nextBoolean(Sources sources,
+ public void nextBoolean(RandomSources sources,
Blackhole bh) {
for (int i = 0; i < numValues; i++) {
bh.consume(sources.getGenerator().nextBoolean());
@@ -111,7 +64,7 @@ public class GenerationPerformance {
* @param bh Data sink.
*/
@Benchmark
- public void nextInt(Sources sources,
+ public void nextInt(RandomSources sources,
Blackhole bh) {
for (int i = 0; i < numValues; i++) {
bh.consume(sources.getGenerator().nextInt());
@@ -123,7 +76,7 @@ public class GenerationPerformance {
* @param bh Data sink.
*/
@Benchmark
- public void nextIntN(Sources sources,
+ public void nextIntN(RandomSources sources,
Blackhole bh) {
final int n = 10;
for (int i = 0; i < numValues; i++) {
@@ -136,7 +89,7 @@ public class GenerationPerformance {
* @param bh Data sink.
*/
@Benchmark
- public void nextLong(Sources sources,
+ public void nextLong(RandomSources sources,
Blackhole bh) {
for (int i = 0; i < numValues; i++) {
bh.consume(sources.getGenerator().nextLong());
@@ -148,7 +101,7 @@ public class GenerationPerformance {
* @param bh Data sink.
*/
@Benchmark
- public void nextLongN(Sources sources,
+ public void nextLongN(RandomSources sources,
Blackhole bh) {
final long n = 2L * Integer.MAX_VALUE;
for (int i = 0; i < numValues; i++) {
@@ -161,7 +114,7 @@ public class GenerationPerformance {
* @param bh Data sink.
*/
@Benchmark
- public void nextFloat(Sources sources,
+ public void nextFloat(RandomSources sources,
Blackhole bh) {
for (int i = 0; i < numValues; i++) {
bh.consume(sources.getGenerator().nextFloat());
@@ -173,7 +126,7 @@ public class GenerationPerformance {
* @param bh Data sink.
*/
@Benchmark
- public void nextDouble(Sources sources,
+ public void nextDouble(RandomSources sources,
Blackhole bh) {
for (int i = 0; i < numValues; i++) {
bh.consume(sources.getGenerator().nextDouble());
@@ -185,7 +138,7 @@ public class GenerationPerformance {
* @param bh Data sink.
*/
@Benchmark
- public void nextBytes(Sources sources,
+ public void nextBytes(RandomSources sources,
Blackhole bh) {
final byte[] result = new byte[numValues];
sources.getGenerator().nextBytes(result);
diff --git a/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/RandomSources.java b/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/RandomSourceValues.java
similarity index 75%
copy from commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/RandomSources.java
copy to commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/RandomSourceValues.java
index cdfdca8..823162c 100644
--- a/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/RandomSources.java
+++ b/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/RandomSourceValues.java
@@ -17,7 +17,6 @@
package org.apache.commons.rng.examples.jmh;
-import org.apache.commons.rng.UniformRandomProvider;
import org.apache.commons.rng.simple.RandomSource;
import org.openjdk.jmh.annotations.Param;
import org.openjdk.jmh.annotations.Scope;
@@ -25,12 +24,12 @@ import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
/**
- * A benchmark state that can retrieve the various {@link RandomSource} generators.
+ * A benchmark state that can retrieve the various {@link RandomSource} values.
*
* <p>The state will include only those that do not require additional constructor arguments.</p>
*/
@State(Scope.Benchmark)
-public class RandomSources {
+public class RandomSourceValues {
/**
* RNG providers. This list is maintained in the order of the {@link RandomSource} enum.
*
@@ -56,25 +55,36 @@ public class RandomSources {
"MWC_256",
"KISS",
"XOR_SHIFT_1024_S_PHI",
+ "XO_RO_SHI_RO_64_S",
+ "XO_RO_SHI_RO_64_SS",
+ "XO_SHI_RO_128_PLUS",
+ "XO_SHI_RO_128_SS",
+ "XO_RO_SHI_RO_128_PLUS",
+ "XO_RO_SHI_RO_128_SS",
+ "XO_SHI_RO_256_PLUS",
+ "XO_SHI_RO_256_SS",
+ "XO_SHI_RO_512_PLUS",
+ "XO_SHI_RO_512_SS",
})
private String randomSourceName;
- /** RNG. */
- private UniformRandomProvider generator;
+ /** The RandomSource. */
+ private RandomSource randomSource;
/**
- * Gets the generator.
+ * Gets the random source.
*
- * @return the RNG.
+ * @return the random source
*/
- public UniformRandomProvider getGenerator() {
- return generator;
+ public RandomSource getRandomSource() {
+ return randomSource;
}
- /** Instantiates generator. */
+ /**
+ * Look-up the {@link RandomSource} from the name.
+ */
@Setup
public void setup() {
- final RandomSource randomSource = RandomSource.valueOf(randomSourceName);
- generator = RandomSource.create(randomSource);
+ randomSource = RandomSource.valueOf(randomSourceName);
}
}
diff --git a/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/RandomSources.java b/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/RandomSources.java
index cdfdca8..b8258ee 100644
--- a/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/RandomSources.java
+++ b/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/RandomSources.java
@@ -19,7 +19,6 @@ package org.apache.commons.rng.examples.jmh;
import org.apache.commons.rng.UniformRandomProvider;
import org.apache.commons.rng.simple.RandomSource;
-import org.openjdk.jmh.annotations.Param;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
@@ -30,35 +29,7 @@ import org.openjdk.jmh.annotations.State;
* <p>The state will include only those that do not require additional constructor arguments.</p>
*/
@State(Scope.Benchmark)
-public class RandomSources {
- /**
- * RNG providers. This list is maintained in the order of the {@link RandomSource} enum.
- *
- * <p>Include only those that do not require additional constructor arguments.</p>
- *
- * <p>Note: JMH does support using an Enum for the {@code @Param} annotation. However
- * the default set will encompass all the enum values, including those that require
- * additional constructor arguments. So this list is maintained manually.</p>
- */
- @Param({"JDK",
- "WELL_512_A",
- "WELL_1024_A",
- "WELL_19937_A",
- "WELL_19937_C",
- "WELL_44497_A",
- "WELL_44497_B",
- "MT",
- "ISAAC",
- "SPLIT_MIX_64",
- "XOR_SHIFT_1024_S",
- "TWO_CMRES",
- "MT_64",
- "MWC_256",
- "KISS",
- "XOR_SHIFT_1024_S_PHI",
- })
- private String randomSourceName;
-
+public class RandomSources extends RandomSourceValues {
/** RNG. */
private UniformRandomProvider generator;
@@ -71,10 +42,13 @@ public class RandomSources {
return generator;
}
- /** Instantiates generator. */
+ /**
+ * Look-up the {@link RandomSource} from the name and instantiates the generator.
+ */
+ @Override
@Setup
public void setup() {
- final RandomSource randomSource = RandomSource.valueOf(randomSourceName);
- generator = RandomSource.create(randomSource);
+ super.setup();
+ generator = RandomSource.create(getRandomSource());
}
}
diff --git a/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/distribution/GeometricSamplersPerformance.java b/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/distribution/GeometricSamplersPerformance.java
index 239cb4a..b87ef8f 100644
--- a/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/distribution/GeometricSamplersPerformance.java
+++ b/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/distribution/GeometricSamplersPerformance.java
@@ -57,7 +57,7 @@ public class GeometricSamplersPerformance {
/**
* RNG providers.
*
- * <p>Use different speeds.
+ * <p>Use different speeds.</p>
*
* @see <a href="https://commons.apache.org/proper/commons-rng/userguide/rng.html">
* Commons RNG user guide</a>
diff --git a/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/distribution/NextGaussianPerformance.java b/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/distribution/NextGaussianPerformance.java
index 4e5e1e8..0b89a47 100644
--- a/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/distribution/NextGaussianPerformance.java
+++ b/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/distribution/NextGaussianPerformance.java
@@ -46,7 +46,7 @@ public class NextGaussianPerformance {
/**
* The value.
*
- * <p>This must NOT be final!
+ * <p>This must NOT be final!</p>
*/
private double value;
diff --git a/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/distribution/PoissonSamplerCachePerformance.java b/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/distribution/PoissonSamplerCachePerformance.java
index 38826fb..6702711 100644
--- a/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/distribution/PoissonSamplerCachePerformance.java
+++ b/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/distribution/PoissonSamplerCachePerformance.java
@@ -183,7 +183,7 @@ public class PoissonSamplerCachePerformance {
/**
* RNG providers.
*
- * <p>Use different speeds.
+ * <p>Use different speeds.</p>
*
* @see <a href="https://commons.apache.org/proper/commons-rng/userguide/rng.html">
* Commons RNG user guide</a>
diff --git a/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/distribution/SamplersPerformance.java b/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/distribution/SamplersPerformance.java
index ad2d096..727b00b 100644
--- a/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/distribution/SamplersPerformance.java
+++ b/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/distribution/SamplersPerformance.java
@@ -25,14 +25,11 @@ import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Scope;
-import org.openjdk.jmh.annotations.Param;
-import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.infra.Blackhole;
import java.util.concurrent.TimeUnit;
-import org.apache.commons.rng.UniformRandomProvider;
-import org.apache.commons.rng.simple.RandomSource;
+import org.apache.commons.rng.examples.jmh.RandomSources;
import org.apache.commons.rng.sampling.distribution.ContinuousSampler;
import org.apache.commons.rng.sampling.distribution.DiscreteSampler;
import org.apache.commons.rng.sampling.distribution.BoxMullerNormalizedGaussianSampler;
@@ -63,49 +60,6 @@ public class SamplersPerformance {
private static final int NUM_SAMPLES = 10000000;
/**
- * The benchmark state (retrieve the various "RandomSource"s).
- */
- @State(Scope.Benchmark)
- public static class Sources {
- /**
- * RNG providers.
- */
- @Param({"JDK",
- "WELL_512_A",
- "WELL_1024_A",
- "WELL_19937_A",
- "WELL_19937_C",
- "WELL_44497_A",
- "WELL_44497_B",
- "MT",
- "ISAAC",
- "SPLIT_MIX_64",
- "MWC_256",
- "KISS",
- "XOR_SHIFT_1024_S",
- "TWO_CMRES",
- "MT_64" })
- private String randomSourceName;
-
- /** RNG. */
- private UniformRandomProvider generator;
-
- /**
- * @return the RNG.
- */
- public UniformRandomProvider getGenerator() {
- return generator;
- }
-
- /** Intantiates generator. */
- @Setup
- public void setup() {
- final RandomSource randomSource = RandomSource.valueOf(randomSourceName);
- generator = RandomSource.create(randomSource);
- }
- }
-
- /**
* Exercises a continuous sampler.
*
* @param sampler Sampler.
@@ -138,7 +92,7 @@ public class SamplersPerformance {
* @param bh Data sink.
*/
@Benchmark
- public void runBoxMullerNormalizedGaussianSampler(Sources sources,
+ public void runBoxMullerNormalizedGaussianSampler(RandomSources sources,
Blackhole bh) {
runSample(new BoxMullerNormalizedGaussianSampler(sources.getGenerator()), bh);
}
@@ -148,7 +102,7 @@ public class SamplersPerformance {
* @param bh Data sink.
*/
@Benchmark
- public void runMarsagliaNormalizedGaussianSampler(Sources sources,
+ public void runMarsagliaNormalizedGaussianSampler(RandomSources sources,
Blackhole bh) {
runSample(new MarsagliaNormalizedGaussianSampler(sources.getGenerator()), bh);
}
@@ -158,7 +112,7 @@ public class SamplersPerformance {
* @param bh Data sink.
*/
@Benchmark
- public void runZigguratNormalizedGaussianSampler(Sources sources,
+ public void runZigguratNormalizedGaussianSampler(RandomSources sources,
Blackhole bh) {
runSample(new ZigguratNormalizedGaussianSampler(sources.getGenerator()), bh);
}
@@ -168,7 +122,7 @@ public class SamplersPerformance {
* @param bh Data sink.
*/
@Benchmark
- public void runAhrensDieterExponentialSampler(Sources sources,
+ public void runAhrensDieterExponentialSampler(RandomSources sources,
Blackhole bh) {
runSample(new AhrensDieterExponentialSampler(sources.getGenerator(), 4.56), bh);
}
@@ -178,7 +132,7 @@ public class SamplersPerformance {
* @param bh Data sink.
*/
@Benchmark
- public void runAhrensDieterMarsagliaTsangGammaSampler(Sources sources,
+ public void runAhrensDieterMarsagliaTsangGammaSampler(RandomSources sources,
Blackhole bh) {
runSample(new AhrensDieterMarsagliaTsangGammaSampler(sources.getGenerator(), 9.8, 0.76), bh);
}
@@ -188,7 +142,7 @@ public class SamplersPerformance {
* @param bh Data sink.
*/
@Benchmark
- public void runBoxMullerLogNormalSampler(Sources sources,
+ public void runBoxMullerLogNormalSampler(RandomSources sources,
Blackhole bh) {
runSample(new LogNormalSampler(new BoxMullerNormalizedGaussianSampler(sources.getGenerator()), 12.3, 4.6), bh);
}
@@ -198,7 +152,7 @@ public class SamplersPerformance {
* @param bh Data sink.
*/
@Benchmark
- public void runMarsagliaLogNormalSampler(Sources sources,
+ public void runMarsagliaLogNormalSampler(RandomSources sources,
Blackhole bh) {
runSample(new LogNormalSampler(new MarsagliaNormalizedGaussianSampler(sources.getGenerator()), 12.3, 4.6), bh);
}
@@ -208,7 +162,7 @@ public class SamplersPerformance {
* @param bh Data sink.
*/
@Benchmark
- public void runZigguratLogNormalSampler(Sources sources,
+ public void runZigguratLogNormalSampler(RandomSources sources,
Blackhole bh) {
runSample(new LogNormalSampler(new ZigguratNormalizedGaussianSampler(sources.getGenerator()), 12.3, 4.6), bh);
}
@@ -218,7 +172,7 @@ public class SamplersPerformance {
* @param bh Data sink.
*/
@Benchmark
- public void runChengBetaSampler(Sources sources,
+ public void runChengBetaSampler(RandomSources sources,
Blackhole bh) {
runSample(new ChengBetaSampler(sources.getGenerator(), 0.45, 6.7), bh);
}
@@ -228,7 +182,7 @@ public class SamplersPerformance {
* @param bh Data sink.
*/
@Benchmark
- public void runContinuousUniformSampler(Sources sources,
+ public void runContinuousUniformSampler(RandomSources sources,
Blackhole bh) {
runSample(new ContinuousUniformSampler(sources.getGenerator(), 123.4, 5678.9), bh);
}
@@ -238,7 +192,7 @@ public class SamplersPerformance {
* @param bh Data sink.
*/
@Benchmark
- public void runDiscreteUniformSampler(Sources sources,
+ public void runDiscreteUniformSampler(RandomSources sources,
Blackhole bh) {
runSample(new DiscreteUniformSampler(sources.getGenerator(), -98, 76), bh);
}
@@ -248,7 +202,7 @@ public class SamplersPerformance {
* @param bh Data sink.
*/
@Benchmark
- public void runRejectionInversionZipfSampler(Sources sources,
+ public void runRejectionInversionZipfSampler(RandomSources sources,
Blackhole bh) {
runSample(new RejectionInversionZipfSampler(sources.getGenerator(), 43, 2.1), bh);
}
@@ -258,7 +212,7 @@ public class SamplersPerformance {
* @param bh Data sink.
*/
@Benchmark
- public void runPoissonSampler(Sources sources,
+ public void runPoissonSampler(RandomSources sources,
Blackhole bh) {
runSample(new PoissonSampler(sources.getGenerator(), 8.9), bh);
}
@@ -268,7 +222,7 @@ public class SamplersPerformance {
* @param bh Data sink.
*/
@Benchmark
- public void runGeometricSampler(Sources sources,
+ public void runGeometricSampler(RandomSources sources,
Blackhole bh) {
runSample(new GeometricSampler(sources.getGenerator(), 0.21), bh);
}