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/09 15:54:18 UTC
[commons-rng] 02/05: Add Continuous and Discrete samplers
benchmarks.
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 b31ada7699ffe782cebeccd45dcec33e404970df
Author: aherbert <ah...@apache.org>
AuthorDate: Wed Apr 3 15:13:13 2019 +0100
Add Continuous and Discrete samplers benchmarks.
---
.../commons/rng/examples/jmh/RandomSources.java | 80 +++++++++++
.../ContinuousSamplersPerformance.java | 153 +++++++++++++++++++++
.../distribution/DiscreteSamplersPerformance.java | 132 ++++++++++++++++++
.../distribution/GeometricSamplersPerformance.java | 138 ++++++++-----------
.../jmh/distribution/NextGaussianPerformance.java | 34 +++--
5 files changed, 443 insertions(+), 94 deletions(-)
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
new file mode 100644
index 0000000..cdfdca8
--- /dev/null
+++ b/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/RandomSources.java
@@ -0,0 +1,80 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+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;
+
+/**
+ * A benchmark state that can retrieve the various {@link RandomSource} generators.
+ *
+ * <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;
+
+ /** RNG. */
+ private UniformRandomProvider generator;
+
+ /**
+ * Gets the generator.
+ *
+ * @return the RNG.
+ */
+ public UniformRandomProvider getGenerator() {
+ return generator;
+ }
+
+ /** Instantiates generator. */
+ @Setup
+ public void setup() {
+ final RandomSource randomSource = RandomSource.valueOf(randomSourceName);
+ generator = RandomSource.create(randomSource);
+ }
+}
diff --git a/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/distribution/ContinuousSamplersPerformance.java b/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/distribution/ContinuousSamplersPerformance.java
new file mode 100644
index 0000000..1be0ee0
--- /dev/null
+++ b/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/distribution/ContinuousSamplersPerformance.java
@@ -0,0 +1,153 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.rng.examples.jmh.distribution;
+
+import org.apache.commons.rng.UniformRandomProvider;
+import org.apache.commons.rng.examples.jmh.RandomSources;
+import org.apache.commons.rng.sampling.distribution.AhrensDieterExponentialSampler;
+import org.apache.commons.rng.sampling.distribution.AhrensDieterMarsagliaTsangGammaSampler;
+import org.apache.commons.rng.sampling.distribution.BoxMullerNormalizedGaussianSampler;
+import org.apache.commons.rng.sampling.distribution.ChengBetaSampler;
+import org.apache.commons.rng.sampling.distribution.ContinuousSampler;
+import org.apache.commons.rng.sampling.distribution.ContinuousUniformSampler;
+import org.apache.commons.rng.sampling.distribution.InverseTransformParetoSampler;
+import org.apache.commons.rng.sampling.distribution.LogNormalSampler;
+import org.apache.commons.rng.sampling.distribution.MarsagliaNormalizedGaussianSampler;
+import org.apache.commons.rng.sampling.distribution.ZigguratNormalizedGaussianSampler;
+import org.apache.commons.rng.simple.RandomSource;
+
+import org.openjdk.jmh.annotations.Benchmark;
+import org.openjdk.jmh.annotations.BenchmarkMode;
+import org.openjdk.jmh.annotations.Fork;
+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;
+import org.openjdk.jmh.annotations.Warmup;
+
+import java.util.concurrent.TimeUnit;
+
+/**
+ * Executes benchmark to compare the speed of generation of random numbers
+ * from the various source providers for different types of {@link ContinuousSampler}.
+ */
+@BenchmarkMode(Mode.AverageTime)
+@OutputTimeUnit(TimeUnit.NANOSECONDS)
+@Warmup(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
+@Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
+@State(Scope.Benchmark)
+@Fork(value = 1, jvmArgs = {"-server", "-Xms128M", "-Xmx128M"})
+public class ContinuousSamplersPerformance {
+ /**
+ * The {@link ContinuousSampler} samplers to use for testing. Creates the sampler for each
+ * {@link RandomSource} in the default {@link RandomSources}.
+ */
+ @State(Scope.Benchmark)
+ public static class Sources extends RandomSources {
+ /**
+ * The sampler type.
+ */
+ @Param({"BoxMullerNormalizedGaussianSampler",
+ "MarsagliaNormalizedGaussianSampler",
+ "ZigguratNormalizedGaussianSampler",
+ "AhrensDieterExponentialSampler",
+ "AhrensDieterMarsagliaTsangGammaSampler",
+ "LogNormalBoxMullerNormalizedGaussianSampler",
+ "LogNormalMarsagliaNormalizedGaussianSampler",
+ "LogNormalZigguratNormalizedGaussianSampler",
+ "ChengBetaSampler",
+ "ContinuousUniformSampler",
+ "InverseTransformParetoSampler",
+ })
+ private String samplerType;
+
+ /** The sampler. */
+ private ContinuousSampler sampler;
+
+ /**
+ * @return the sampler.
+ */
+ public ContinuousSampler getSampler() {
+ return sampler;
+ }
+
+ /** Instantiates sampler. */
+ @Override
+ @Setup
+ public void setup() {
+ super.setup();
+ final UniformRandomProvider rng = getGenerator();
+ if ("BoxMullerNormalizedGaussianSampler".equals(samplerType)) {
+ sampler = new BoxMullerNormalizedGaussianSampler(rng);
+ } else if ("MarsagliaNormalizedGaussianSampler".equals(samplerType)) {
+ sampler = new MarsagliaNormalizedGaussianSampler(rng);
+ } else if ("ZigguratNormalizedGaussianSampler".equals(samplerType)) {
+ sampler = new ZigguratNormalizedGaussianSampler(rng);
+ } else if ("AhrensDieterExponentialSampler".equals(samplerType)) {
+ sampler = new AhrensDieterExponentialSampler(rng, 4.56);
+ } else if ("AhrensDieterMarsagliaTsangGammaSampler".equals(samplerType)) {
+ sampler = new AhrensDieterMarsagliaTsangGammaSampler(rng, 9.8, 0.76);
+ } else if ("LogNormalBoxMullerNormalizedGaussianSampler".equals(samplerType)) {
+ sampler = new LogNormalSampler(new BoxMullerNormalizedGaussianSampler(rng), 12.3, 4.6);
+ } else if ("LogNormalMarsagliaNormalizedGaussianSampler".equals(samplerType)) {
+ sampler = new LogNormalSampler(new MarsagliaNormalizedGaussianSampler(rng), 12.3, 4.6);
+ } else if ("LogNormalZigguratNormalizedGaussianSampler".equals(samplerType)) {
+ sampler = new LogNormalSampler(new ZigguratNormalizedGaussianSampler(rng), 12.3, 4.6);
+ } else if ("ChengBetaSampler".equals(samplerType)) {
+ sampler = new ChengBetaSampler(rng, 0.45, 6.7);
+ } else if ("ContinuousUniformSampler".equals(samplerType)) {
+ sampler = new ContinuousUniformSampler(rng, 123.4, 5678.9);
+ } else if ("InverseTransformParetoSampler".equals(samplerType)) {
+ sampler = new InverseTransformParetoSampler(rng, 23.45, 0.1234);
+ }
+ }
+ }
+
+ // Benchmarks methods below.
+
+ /**
+ * The value.
+ *
+ * <p>This must NOT be final!</p>
+ */
+ private double value;
+
+ /**
+ * Baseline for the JMH timing overhead for production of an {@code double} value.
+ *
+ * @return the {@code double} value
+ */
+ @Benchmark
+ public double baseline() {
+ return value;
+ }
+
+ /**
+ * Run the sampler.
+ *
+ * @param sources Source of randomness.
+ * @return the sample value
+ */
+ @Benchmark
+ public double sample(Sources sources) {
+ return sources.getSampler().sample();
+ }
+}
diff --git a/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/distribution/DiscreteSamplersPerformance.java b/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/distribution/DiscreteSamplersPerformance.java
new file mode 100644
index 0000000..934c934
--- /dev/null
+++ b/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/distribution/DiscreteSamplersPerformance.java
@@ -0,0 +1,132 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.rng.examples.jmh.distribution;
+
+import org.apache.commons.rng.UniformRandomProvider;
+import org.apache.commons.rng.examples.jmh.RandomSources;
+import org.apache.commons.rng.sampling.distribution.DiscreteSampler;
+import org.apache.commons.rng.sampling.distribution.DiscreteUniformSampler;
+import org.apache.commons.rng.sampling.distribution.GeometricSampler;
+import org.apache.commons.rng.sampling.distribution.LargeMeanPoissonSampler;
+import org.apache.commons.rng.sampling.distribution.RejectionInversionZipfSampler;
+import org.apache.commons.rng.sampling.distribution.SmallMeanPoissonSampler;
+import org.apache.commons.rng.simple.RandomSource;
+
+import org.openjdk.jmh.annotations.Benchmark;
+import org.openjdk.jmh.annotations.BenchmarkMode;
+import org.openjdk.jmh.annotations.Fork;
+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;
+import org.openjdk.jmh.annotations.Warmup;
+
+import java.util.concurrent.TimeUnit;
+
+/**
+ * Executes benchmark to compare the speed of generation of random numbers
+ * from the various source providers for different types of {@link DiscreteSampler}.
+ */
+@BenchmarkMode(Mode.AverageTime)
+@OutputTimeUnit(TimeUnit.NANOSECONDS)
+@Warmup(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
+@Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
+@State(Scope.Benchmark)
+@Fork(value = 1, jvmArgs = {"-server", "-Xms128M", "-Xmx128M"})
+public class DiscreteSamplersPerformance {
+ /**
+ * The {@link DiscreteSampler} samplers to use for testing. Creates the sampler for each
+ * {@link RandomSource} in the default {@link RandomSources}.
+ */
+ @State(Scope.Benchmark)
+ public static class Sources extends RandomSources {
+ /**
+ * The sampler type.
+ */
+ @Param({"DiscreteUniformSampler",
+ "RejectionInversionZipfSampler",
+ "SmallMeanPoissonSampler",
+ "LargeMeanPoissonSampler",
+ "GeometricSampler",
+ })
+ private String samplerType;
+
+ /** The sampler. */
+ private DiscreteSampler sampler;
+
+ /**
+ * @return the sampler.
+ */
+ public DiscreteSampler getSampler() {
+ return sampler;
+ }
+
+ /** Instantiates sampler. */
+ @Override
+ @Setup
+ public void setup() {
+ super.setup();
+ final UniformRandomProvider rng = getGenerator();
+ if ("DiscreteUniformSampler".equals(samplerType)) {
+ sampler = new DiscreteUniformSampler(rng, -98, 76);
+ } else if ("RejectionInversionZipfSampler".equals(samplerType)) {
+ sampler = new RejectionInversionZipfSampler(rng, 43, 2.1);
+ } else if ("SmallMeanPoissonSampler".equals(samplerType)) {
+ sampler = new SmallMeanPoissonSampler(rng, 8.9);
+ } else if ("LargeMeanPoissonSampler".equals(samplerType)) {
+ // Note: Use with a fractional part to the mean includes a small mean sample
+ sampler = new LargeMeanPoissonSampler(rng, 41.7);
+ } else if ("GeometricSampler".equals(samplerType)) {
+ sampler = new GeometricSampler(rng, 0.21);
+ }
+ }
+ }
+
+ // Benchmarks methods below.
+
+ /**
+ * The value.
+ *
+ * <p>This must NOT be final!</p>
+ */
+ private int value;
+
+ /**
+ * Baseline for the JMH timing overhead for production of an {@code int} value.
+ *
+ * @return the {@code int} value
+ */
+ @Benchmark
+ public int baseline() {
+ return value;
+ }
+
+ /**
+ * Run the sampler.
+ *
+ * @param sources Source of randomness.
+ * @return the sample value
+ */
+ @Benchmark
+ public int sample(Sources sources) {
+ return sources.getSampler().sample();
+ }
+}
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 bf3cdf9..239cb4a 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
@@ -17,90 +17,90 @@
package org.apache.commons.rng.examples.jmh.distribution;
+import org.apache.commons.rng.UniformRandomProvider;
+import org.apache.commons.rng.sampling.distribution.DiscreteInverseCumulativeProbabilityFunction;
+import org.apache.commons.rng.sampling.distribution.DiscreteSampler;
+import org.apache.commons.rng.sampling.distribution.GeometricSampler;
+import org.apache.commons.rng.sampling.distribution.InverseTransformDiscreteSampler;
+import org.apache.commons.rng.simple.RandomSource;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
-import org.openjdk.jmh.annotations.Mode;
-import org.openjdk.jmh.annotations.Warmup;
-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.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.OutputTimeUnit;
-import org.openjdk.jmh.infra.Blackhole;
-import java.util.concurrent.TimeUnit;
+import org.openjdk.jmh.annotations.State;
+import org.openjdk.jmh.annotations.Warmup;
-import org.apache.commons.rng.UniformRandomProvider;
-import org.apache.commons.rng.simple.RandomSource;
-import org.apache.commons.rng.sampling.distribution.DiscreteInverseCumulativeProbabilityFunction;
-import org.apache.commons.rng.sampling.distribution.DiscreteSampler;
-import org.apache.commons.rng.sampling.distribution.GeometricSampler;
-import org.apache.commons.rng.sampling.distribution.InverseTransformDiscreteSampler;
+import java.util.concurrent.TimeUnit;
/**
* Executes a benchmark to compare the speed of generation of Geometric random numbers
* using different methods.
*/
@BenchmarkMode(Mode.AverageTime)
-@OutputTimeUnit(TimeUnit.MICROSECONDS)
+@OutputTimeUnit(TimeUnit.NANOSECONDS)
@Warmup(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
@Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
@State(Scope.Benchmark)
@Fork(value = 1, jvmArgs = {"-server", "-Xms128M", "-Xmx128M"})
public class GeometricSamplersPerformance {
- /** Number of samples per run. */
- private static final int NUM_SAMPLES = 10000000;
-
/**
- * The RandomSource's to use for testing.
+ * The samplers's to use for testing. Defines the RandomSource, probability of success
+ * and the type of Geometric sampler.
*/
@State(Scope.Benchmark)
public static class Sources {
/**
* RNG providers.
+ *
+ * <p>Use different speeds.
+ *
+ * @see <a href="https://commons.apache.org/proper/commons-rng/userguide/rng.html">
+ * Commons RNG user guide</a>
*/
@Param({"SPLIT_MIX_64",
"MWC_256",
- "JDK" })
+ "JDK"})
private String randomSourceName;
- /** RNG. */
- private UniformRandomProvider generator;
-
/**
- * @return the RNG.
+ * The probability of success.
*/
- public UniformRandomProvider getGenerator() {
- return generator;
- }
+ @Param({"0.1", "0.3"})
+ private double probabilityOfSuccess;
- /** Instantiates generator. */
- @Setup
- public void setup() {
- final RandomSource randomSource = RandomSource.valueOf(randomSourceName);
- generator = RandomSource.create(randomSource);
- }
- }
-
- /**
- * The probability of success for testing.
- */
- @State(Scope.Benchmark)
- public static class ProbabilityOfSuccess {
/**
- * The probability.
+ * The sampler type.
*/
- @Param({ "0.1", "0.3"})
- private double probability;
+ @Param({"GeometricSampler", "InverseTransformDiscreteSampler"})
+ private String samplerType;
+
+ /** The sampler. */
+ private DiscreteSampler sampler;
/**
- * Gets the probability of success.
- *
- * @return the probability of success
+ * @return the sampler.
*/
- public double getProbability() {
- return probability;
+ public DiscreteSampler getSampler() {
+ return sampler;
+ }
+
+ /** Instantiates sampler. */
+ @Setup
+ public void setup() {
+ final RandomSource randomSource = RandomSource.valueOf(randomSourceName);
+ final UniformRandomProvider rng = RandomSource.create(randomSource);
+ if ("GeometricSampler".equals(samplerType)) {
+ sampler = new GeometricSampler(rng, probabilityOfSuccess);
+ } else {
+ final DiscreteInverseCumulativeProbabilityFunction geometricFunction =
+ new GeometricDiscreteInverseCumulativeProbabilityFunction(probabilityOfSuccess);
+ sampler = new InverseTransformDiscreteSampler(rng, geometricFunction);
+ }
}
}
@@ -142,50 +142,30 @@ public class GeometricSamplersPerformance {
}
/**
- * Exercises a discrete sampler.
+ * The value.
*
- * @param sampler Sampler.
- * @param bh Data sink.
+ * <p>This must NOT be final!</p>
*/
- private static void runSample(DiscreteSampler sampler,
- Blackhole bh) {
- for (int i = 0; i < NUM_SAMPLES; i++) {
- bh.consume(sampler.sample());
- }
- }
-
- // Benchmarks methods below.
+ private int value;
/**
- * Run geometric sampler.
+ * Baseline for the JMH timing overhead for production of an {@code int} value.
*
- * @param sources Source of randomness.
- * @param success The probability of success.
- * @param bh Data sink.
+ * @return the {@code int} value
*/
@Benchmark
- public void runGeometricSampler(Sources sources,
- ProbabilityOfSuccess success,
- Blackhole bh) {
- runSample(new GeometricSampler(sources.getGenerator(), success.getProbability()), bh);
+ public int baseline() {
+ return value;
}
/**
- * Run geometric sampler.
+ * Run the sampler.
*
* @param sources Source of randomness.
- * @param success The probability of success.
- * @param bh Data sink.
+ * @return the sample value
*/
@Benchmark
- public void runGeometricInverseTranformSampler(Sources sources,
- ProbabilityOfSuccess success,
- Blackhole bh) {
- final DiscreteInverseCumulativeProbabilityFunction geometricFunction =
- new GeometricDiscreteInverseCumulativeProbabilityFunction(success.getProbability());
- final DiscreteSampler inverseMethodSampler =
- new InverseTransformDiscreteSampler(sources.getGenerator(),
- geometricFunction);
- runSample(inverseMethodSampler, bh);
+ public int sample(Sources sources) {
+ return sources.getSampler().sample();
}
}
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 2b65432..4e5e1e8 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
@@ -26,7 +26,6 @@ import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.OutputTimeUnit;
-import org.openjdk.jmh.infra.Blackhole;
import java.util.concurrent.TimeUnit;
import java.util.Random;
@@ -35,34 +34,39 @@ import java.util.Random;
* the speed of generation of normally-distributed random numbers.
*/
@BenchmarkMode(Mode.AverageTime)
-@OutputTimeUnit(TimeUnit.MICROSECONDS)
+@OutputTimeUnit(TimeUnit.NANOSECONDS)
@Warmup(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
@Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
@State(Scope.Benchmark)
@Fork(value = 1, jvmArgs = {"-server", "-Xms128M", "-Xmx128M"})
public class NextGaussianPerformance {
- /** Number of samples per run. */
- private static final int NUM_SAMPLES = 10000000;
/** JDK's generator. */
private final Random random = new Random();
+
/**
- * Exercises the JDK's Gaussian sampler.
+ * The value.
*
- * @param bh Data sink.
+ * <p>This must NOT be final!
*/
- private void runSample(Blackhole bh) {
- for (int i = 0; i < NUM_SAMPLES; i++) {
- bh.consume(random.nextGaussian());
- }
- }
+ private double value;
- // Benchmarks methods below.
+ /**
+ * Baseline for the JMH timing overhead for production of an {@code double} value.
+ *
+ * @return the {@code double} value
+ */
+ @Benchmark
+ public double baseline() {
+ return value;
+ }
/**
- * @param bh Data sink.
+ * Run JDK random gaussian sampler.
+ *
+ * @return the double
*/
@Benchmark
- public void runJDKRandomGaussianSampler(Blackhole bh) {
- runSample(bh);
+ public double runJDKRandomGaussianSampler() {
+ return random.nextGaussian();
}
}