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 da20fc81d265df172d595e70d5debf1f9d694a17 Author: Alex Herbert <[email protected]> AuthorDate: Fri Aug 13 20:31:42 2021 +0100 Replace benchmarking interfaces with java.util.function --- .../jmh/sampling/UnitSphereSamplerBenchmark.java | 49 +++++++--------------- .../EnumeratedDistributionSamplersPerformance.java | 19 ++------- .../PoissonSamplerCachePerformance.java | 25 +++-------- .../distribution/PoissonSamplersPerformance.java | 19 ++------- .../distribution/ZigguratSamplerPerformance.java | 11 +---- .../shape/TetrahedronSamplerBenchmark.java | 29 ++++--------- .../sampling/shape/TriangleSamplerBenchmark.java | 39 +++++++---------- .../sampling/shape/UnitBallSamplerBenchmark.java | 33 +++++---------- 8 files changed, 65 insertions(+), 159 deletions(-) diff --git a/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/sampling/UnitSphereSamplerBenchmark.java b/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/sampling/UnitSphereSamplerBenchmark.java index 154d281..9ecc68a 100644 --- a/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/sampling/UnitSphereSamplerBenchmark.java +++ b/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/sampling/UnitSphereSamplerBenchmark.java @@ -18,6 +18,7 @@ package org.apache.commons.rng.examples.jmh.sampling; import org.apache.commons.rng.UniformRandomProvider; +import org.apache.commons.rng.sampling.ObjectSampler; import org.apache.commons.rng.sampling.distribution.NormalizedGaussianSampler; import org.apache.commons.rng.sampling.distribution.ZigguratSampler; import org.apache.commons.rng.simple.RandomSource; @@ -56,18 +57,6 @@ public class UnitSphereSamplerBenchmark { private static final String UNKNOWN_SAMPLER = "Unknown sampler type: "; /** - * The sampler. - */ - private interface Sampler { - /** - * Gets the next sample. - * - * @return the sample - */ - double[] sample(); - } - - /** * Base class for the sampler data. * Contains the source of randomness and the number of samples. * The sampler should be created by a sub-class of the data. @@ -75,7 +64,7 @@ public class UnitSphereSamplerBenchmark { @State(Scope.Benchmark) public abstract static class SamplerData { /** The sampler. */ - private Sampler sampler; + private ObjectSampler<double[]> sampler; /** The number of samples. */ @Param({"100"}) @@ -95,7 +84,7 @@ public class UnitSphereSamplerBenchmark { * * @return the sampler */ - public Sampler getSampler() { + public ObjectSampler<double[]> getSampler() { return sampler; } @@ -115,7 +104,7 @@ public class UnitSphereSamplerBenchmark { * @param rng the source of randomness * @return the sampler */ - protected abstract Sampler createSampler(UniformRandomProvider rng); + protected abstract ObjectSampler<double[]> createSampler(UniformRandomProvider rng); } /** @@ -142,7 +131,7 @@ public class UnitSphereSamplerBenchmark { /** {@inheritDoc} */ @Override - protected Sampler createSampler(final UniformRandomProvider rng) { + protected ObjectSampler<double[]> createSampler(final UniformRandomProvider rng) { if (BASELINE.equals(type)) { return () -> { return new double[] {1.0}; @@ -185,11 +174,9 @@ public class UnitSphereSamplerBenchmark { /** {@inheritDoc} */ @Override - protected Sampler createSampler(final UniformRandomProvider rng) { + protected ObjectSampler<double[]> createSampler(final UniformRandomProvider rng) { if (BASELINE.equals(type)) { - return () -> { - return new double[] {1.0, 0.0}; - }; + return () -> new double[] {1.0, 0.0}; } else if (ARRAY.equals(type)) { return new ArrayBasedUnitSphereSampler(2, rng); } else if (NON_ARRAY.equals(type)) { @@ -201,7 +188,7 @@ public class UnitSphereSamplerBenchmark { /** * Sample from a 2D unit sphere. */ - private static class UnitSphereSampler2D implements Sampler { + private static class UnitSphereSampler2D implements ObjectSampler<double[]> { /** Sampler used for generating the individual components of the vectors. */ private final NormalizedGaussianSampler sampler; @@ -240,11 +227,9 @@ public class UnitSphereSamplerBenchmark { /** {@inheritDoc} */ @Override - protected Sampler createSampler(final UniformRandomProvider rng) { + protected ObjectSampler<double[]> createSampler(final UniformRandomProvider rng) { if (BASELINE.equals(type)) { - return () -> { - return new double[] {1.0, 0.0, 0.0}; - }; + return () -> new double[] {1.0, 0.0, 0.0}; } else if (ARRAY.equals(type)) { return new ArrayBasedUnitSphereSampler(3, rng); } else if (NON_ARRAY.equals(type)) { @@ -256,7 +241,7 @@ public class UnitSphereSamplerBenchmark { /** * Sample from a 3D unit sphere. */ - private static class UnitSphereSampler3D implements Sampler { + private static class UnitSphereSampler3D implements ObjectSampler<double[]> { /** Sampler used for generating the individual components of the vectors. */ private final NormalizedGaussianSampler sampler; @@ -296,11 +281,9 @@ public class UnitSphereSamplerBenchmark { /** {@inheritDoc} */ @Override - protected Sampler createSampler(final UniformRandomProvider rng) { + protected ObjectSampler<double[]> createSampler(final UniformRandomProvider rng) { if (BASELINE.equals(type)) { - return () -> { - return new double[] {1.0, 0.0, 0.0, 0.0}; - }; + return () -> new double[] {1.0, 0.0, 0.0, 0.0}; } else if (ARRAY.equals(type)) { return new ArrayBasedUnitSphereSampler(4, rng); } else if (NON_ARRAY.equals(type)) { @@ -312,7 +295,7 @@ public class UnitSphereSamplerBenchmark { /** * Sample from a 4D unit hypersphere. */ - private static class UnitSphereSampler4D implements Sampler { + private static class UnitSphereSampler4D implements ObjectSampler<double[]> { /** Sampler used for generating the individual components of the vectors. */ private final NormalizedGaussianSampler sampler; @@ -345,7 +328,7 @@ public class UnitSphereSamplerBenchmark { /** * Sample from a unit sphere using an array based method. */ - private static class ArrayBasedUnitSphereSampler implements Sampler { + private static class ArrayBasedUnitSphereSampler implements ObjectSampler<double[]> { /** Space dimension. */ private final int dimension; /** Sampler used for generating the individual components of the vectors. */ @@ -394,7 +377,7 @@ public class UnitSphereSamplerBenchmark { * @param data Input data. */ private static void runSampler(Blackhole bh, SamplerData data) { - final Sampler sampler = data.getSampler(); + final ObjectSampler<double[]> sampler = data.getSampler(); for (int i = data.getSize() - 1; i >= 0; i--) { bh.consume(sampler.sample()); } diff --git a/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/sampling/distribution/EnumeratedDistributionSamplersPerformance.java b/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/sampling/distribution/EnumeratedDistributionSamplersPerformance.java index 2828f44..c2a7033 100644 --- a/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/sampling/distribution/EnumeratedDistributionSamplersPerformance.java +++ b/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/sampling/distribution/EnumeratedDistributionSamplersPerformance.java @@ -43,6 +43,7 @@ import org.openjdk.jmh.annotations.Warmup; import java.util.Arrays; import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.TimeUnit; +import java.util.function.Supplier; /** * Executes benchmark to compare the speed of generation of random numbers from an enumerated @@ -128,24 +129,12 @@ public class EnumeratedDistributionSamplersPerformance { private String samplerType; /** The factory. */ - private DiscreteSamplerFactory factory; + private Supplier<DiscreteSampler> factory; /** The sampler. */ private DiscreteSampler sampler; /** - * A factory for creating DiscreteSampler objects. - */ - interface DiscreteSamplerFactory { - /** - * Creates the sampler. - * - * @return the sampler - */ - DiscreteSampler create(); - } - - /** * Gets the sampler. * * @return the sampler. @@ -162,7 +151,7 @@ public class EnumeratedDistributionSamplersPerformance { final double[] probabilities = createProbabilities(); createSamplerFactory(getGenerator(), probabilities); - sampler = factory.create(); + sampler = factory.get(); } /** @@ -210,7 +199,7 @@ public class EnumeratedDistributionSamplersPerformance { * @return The sampler. */ public DiscreteSampler createSampler() { - return factory.create(); + return factory.get(); } } diff --git a/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/sampling/distribution/PoissonSamplerCachePerformance.java b/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/sampling/distribution/PoissonSamplerCachePerformance.java index 4495374..6cffb72 100644 --- a/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/sampling/distribution/PoissonSamplerCachePerformance.java +++ b/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/sampling/distribution/PoissonSamplerCachePerformance.java @@ -18,7 +18,7 @@ package org.apache.commons.rng.examples.jmh.sampling.distribution; import java.util.concurrent.TimeUnit; - +import java.util.function.DoubleFunction; import org.apache.commons.rng.RandomProviderState; import org.apache.commons.rng.RestorableUniformRandomProvider; import org.apache.commons.rng.UniformRandomProvider; @@ -268,30 +268,17 @@ public class PoissonSamplerCachePerformance { } /** - * A factory for creating Poisson sampler objects. - */ - private interface PoissonSamplerFactory { - /** - * Creates a new Poisson sampler object. - * - * @param mean the mean - * @return The sampler - */ - DiscreteSampler createPoissonSampler(double mean); - } - - /** * Exercises a poisson sampler created for a single use with a range of means. * * @param factory The factory. * @param range The range of means. * @param bh Data sink. */ - private static void runSample(PoissonSamplerFactory factory, + private static void runSample(DoubleFunction<DiscreteSampler> factory, MeanRange range, Blackhole bh) { for (int i = 0; i < NUM_SAMPLES; i++) { - bh.consume(factory.createPoissonSampler(range.getMean(i)).sample()); + bh.consume(factory.apply(range.getMean(i)).sample()); } } @@ -307,7 +294,7 @@ public class PoissonSamplerCachePerformance { MeanRange range, Blackhole bh) { final UniformRandomProvider r = sources.getGenerator(); - final PoissonSamplerFactory factory = mean -> PoissonSampler.of(r, mean); + final DoubleFunction<DiscreteSampler> factory = mean -> PoissonSampler.of(r, mean); runSample(factory, range, bh); } @@ -322,7 +309,7 @@ public class PoissonSamplerCachePerformance { Blackhole bh) { final UniformRandomProvider r = sources.getGenerator(); final PoissonSamplerCache cache = new PoissonSamplerCache(0, 0); - final PoissonSamplerFactory factory = mean -> cache.createSharedStateSampler(r, mean); + final DoubleFunction<DiscreteSampler> factory = mean -> cache.createSharedStateSampler(r, mean); runSample(factory, range, bh); } @@ -338,7 +325,7 @@ public class PoissonSamplerCachePerformance { final UniformRandomProvider r = sources.getGenerator(); final PoissonSamplerCache cache = new PoissonSamplerCache( range.getMin(), range.getMax()); - final PoissonSamplerFactory factory = mean -> cache.createSharedStateSampler(r, mean); + final DoubleFunction<DiscreteSampler> factory = mean -> cache.createSharedStateSampler(r, mean); runSample(factory, range, bh); } } diff --git a/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/sampling/distribution/PoissonSamplersPerformance.java b/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/sampling/distribution/PoissonSamplersPerformance.java index 8537431..54e704b 100644 --- a/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/sampling/distribution/PoissonSamplersPerformance.java +++ b/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/sampling/distribution/PoissonSamplersPerformance.java @@ -37,6 +37,7 @@ import org.openjdk.jmh.annotations.State; import org.openjdk.jmh.annotations.Warmup; import java.util.concurrent.TimeUnit; +import java.util.function.Supplier; /** * Executes benchmark to compare the speed of generation of Poisson distributed random numbers. @@ -136,24 +137,12 @@ public class PoissonSamplersPerformance { private UniformRandomProvider generator; /** The factory. */ - private DiscreteSamplerFactory factory; + private Supplier<DiscreteSampler> factory; /** The sampler. */ private DiscreteSampler sampler; /** - * A factory for creating DiscreteSampler objects. - */ - interface DiscreteSamplerFactory { - /** - * Creates the sampler. - * - * @return the sampler - */ - DiscreteSampler create(); - } - - /** * @return The RNG. */ public UniformRandomProvider getGenerator() { @@ -193,7 +182,7 @@ public class PoissonSamplersPerformance { } else if ("TinyMeanPoissonSampler".equals(samplerType)) { factory = () -> new TinyMeanPoissonSampler(generator, mean); } - sampler = factory.create(); + sampler = factory.get(); } /** @@ -202,7 +191,7 @@ public class PoissonSamplersPerformance { * @return The sampler. */ public DiscreteSampler createSampler() { - return factory.create(); + return factory.get(); } } diff --git a/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/sampling/distribution/ZigguratSamplerPerformance.java b/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/sampling/distribution/ZigguratSamplerPerformance.java index 59a845a..da961d5 100644 --- a/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/sampling/distribution/ZigguratSamplerPerformance.java +++ b/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/sampling/distribution/ZigguratSamplerPerformance.java @@ -20,6 +20,7 @@ package org.apache.commons.rng.examples.jmh.sampling.distribution; import org.apache.commons.rng.UniformRandomProvider; import org.apache.commons.rng.sampling.distribution.ContinuousSampler; import org.apache.commons.rng.sampling.distribution.DiscreteSampler; +import org.apache.commons.rng.sampling.distribution.LongSampler; import org.apache.commons.rng.sampling.distribution.ZigguratSampler; import org.apache.commons.rng.sampling.distribution.ZigguratNormalizedGaussianSampler; import org.apache.commons.rng.simple.RandomSource; @@ -149,16 +150,6 @@ public class ZigguratSamplerPerformance { } /** - * Sampler that generates values of type {@code long}. - */ - interface LongSampler { - /** - * @return a sample. - */ - long sample(); - } - - /** * Defines method to use for creating unsigned {@code long} values. */ @State(Scope.Benchmark) diff --git a/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/sampling/shape/TetrahedronSamplerBenchmark.java b/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/sampling/shape/TetrahedronSamplerBenchmark.java index 5a5f858..2251566 100644 --- a/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/sampling/shape/TetrahedronSamplerBenchmark.java +++ b/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/sampling/shape/TetrahedronSamplerBenchmark.java @@ -18,6 +18,7 @@ package org.apache.commons.rng.examples.jmh.sampling.shape; import org.apache.commons.rng.UniformRandomProvider; +import org.apache.commons.rng.sampling.ObjectSampler; import org.apache.commons.rng.sampling.UnitSphereSampler; import org.apache.commons.rng.simple.RandomSource; import org.openjdk.jmh.annotations.Benchmark; @@ -59,18 +60,6 @@ public class TetrahedronSamplerBenchmark { private static final String UNKNOWN_SAMPLER = "Unknown sampler type: "; /** - * The sampler. - */ - private interface Sampler { - /** - * Gets the next sample. - * - * @return a random Cartesian point within the tetrahedron. - */ - double[] sample(); - } - - /** * The base class for sampling from a tetrahedron. * * <ul> @@ -87,7 +76,7 @@ public class TetrahedronSamplerBenchmark { * @see <a href="https://doi.org/10.1080/10867651.2000.10487528"> * Rocchini, C. & Cignoni, P. (2001) Journal of Graphics Tools 5, pp. 9-12</a> */ - private abstract static class TetrahedronSampler implements Sampler { + private abstract static class TetrahedronSampler implements ObjectSampler<double[]> { /** The source of randomness. */ private final UniformRandomProvider rng; @@ -242,7 +231,7 @@ public class TetrahedronSamplerBenchmark { * Sample from a tetrahedron using array coordinates with an inline sample algorithm * in-place of a method call. */ - private static class ArrayInlineTetrahedronSampler implements Sampler { + private static class ArrayInlineTetrahedronSampler implements ObjectSampler<double[]> { // CHECKSTYLE: stop JavadocVariableCheck private final double[] a; private final double[] b; @@ -310,7 +299,7 @@ public class TetrahedronSamplerBenchmark { * Sample from a tetrahedron using non-array coordinates with an inline sample algorithm * in-place of a method call. */ - private static class NonArrayInlineTetrahedronSampler implements Sampler { + private static class NonArrayInlineTetrahedronSampler implements ObjectSampler<double[]> { // CHECKSTYLE: stop JavadocVariableCheck private final double ax; private final double bx; @@ -396,7 +385,7 @@ public class TetrahedronSamplerBenchmark { @State(Scope.Benchmark) public static class SamplerData { /** The sampler. */ - private Sampler sampler; + private ObjectSampler<double[]> sampler; /** The number of samples. */ @Param({"1", "10", "100", "1000", "10000"}) @@ -420,7 +409,7 @@ public class TetrahedronSamplerBenchmark { * * @return the sampler */ - public Sampler getSampler() { + public ObjectSampler<double[]> getSampler() { return sampler; } @@ -449,8 +438,8 @@ public class TetrahedronSamplerBenchmark { * @param rng the source of randomness * @return the sampler */ - private Sampler createSampler(final UniformRandomProvider rng, - double[] a, double[] b, double[] c, double[] d) { + private ObjectSampler<double[]> createSampler(final UniformRandomProvider rng, + double[] a, double[] b, double[] c, double[] d) { if (BASELINE.equals(type)) { return () -> { final double s = rng.nextDouble(); @@ -479,7 +468,7 @@ public class TetrahedronSamplerBenchmark { */ @Benchmark public void sample(Blackhole bh, SamplerData data) { - final Sampler sampler = data.getSampler(); + final ObjectSampler<double[]> sampler = data.getSampler(); for (int i = data.getSize() - 1; i >= 0; i--) { bh.consume(sampler.sample()); } diff --git a/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/sampling/shape/TriangleSamplerBenchmark.java b/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/sampling/shape/TriangleSamplerBenchmark.java index 67632e9..d033264 100644 --- a/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/sampling/shape/TriangleSamplerBenchmark.java +++ b/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/sampling/shape/TriangleSamplerBenchmark.java @@ -18,6 +18,7 @@ package org.apache.commons.rng.examples.jmh.sampling.shape; import org.apache.commons.rng.UniformRandomProvider; +import org.apache.commons.rng.sampling.ObjectSampler; import org.apache.commons.rng.sampling.UnitSphereSampler; import org.apache.commons.rng.simple.RandomSource; import org.openjdk.jmh.annotations.Benchmark; @@ -57,21 +58,9 @@ public class TriangleSamplerBenchmark { private static final String UNKNOWN_SAMPLER = "Unknown sampler type: "; /** - * The sampler. - */ - private interface Sampler { - /** - * Gets the next sample. - * - * @return the sample - */ - double[] sample(); - } - - /** * Base class for a sampler using vectors: {@code p = a + sv + tw}. */ - private abstract static class VectorTriangleSampler implements Sampler { + private abstract static class VectorTriangleSampler implements ObjectSampler<double[]> { /** The source of randomness. */ private final UniformRandomProvider rng; @@ -115,7 +104,7 @@ public class TriangleSamplerBenchmark { /** * Base class for a sampler using coordinates: {@code a(1 - s - t) + sb + tc}. */ - private abstract static class CoordinateTriangleSampler implements Sampler { + private abstract static class CoordinateTriangleSampler implements ObjectSampler<double[]> { /** The source of randomness. */ private final UniformRandomProvider rng; @@ -172,7 +161,7 @@ public class TriangleSamplerBenchmark { @State(Scope.Benchmark) public abstract static class SamplerData { /** The sampler. */ - private Sampler sampler; + private ObjectSampler<double[]> sampler; /** The number of samples. */ @Param({"1000"}) @@ -192,7 +181,7 @@ public class TriangleSamplerBenchmark { * * @return the sampler */ - public Sampler getSampler() { + public ObjectSampler<double[]> getSampler() { return sampler; } @@ -227,8 +216,8 @@ public class TriangleSamplerBenchmark { * @param c The third vertex. * @return the sampler */ - protected abstract Sampler createSampler(UniformRandomProvider rng, - double[] a, double[] b, double[] c); + protected abstract ObjectSampler<double[]> createSampler(UniformRandomProvider rng, + double[] a, double[] b, double[] c); } /** @@ -248,8 +237,8 @@ public class TriangleSamplerBenchmark { /** {@inheritDoc} */ @Override - protected Sampler createSampler(final UniformRandomProvider rng, - final double[] a, final double[] b, final double[] c) { + protected ObjectSampler<double[]> createSampler(final UniformRandomProvider rng, + final double[] a, final double[] b, final double[] c) { if (BASELINE.equals(type)) { return () -> { final double s = rng.nextDouble(); @@ -362,8 +351,8 @@ public class TriangleSamplerBenchmark { /** {@inheritDoc} */ @Override - protected Sampler createSampler(final UniformRandomProvider rng, - final double[] a, final double[] b, final double[] c) { + protected ObjectSampler<double[]> createSampler(final UniformRandomProvider rng, + final double[] a, final double[] b, final double[] c) { if (BASELINE.equals(type)) { return () -> { final double s = rng.nextDouble(); @@ -494,8 +483,8 @@ public class TriangleSamplerBenchmark { /** {@inheritDoc} */ @Override - protected Sampler createSampler(final UniformRandomProvider rng, - final double[] a, final double[] b, final double[] c) { + protected ObjectSampler<double[]> createSampler(final UniformRandomProvider rng, + final double[] a, final double[] b, final double[] c) { if (BASELINE.equals(type)) { return () -> { double s = rng.nextDouble(); @@ -614,7 +603,7 @@ public class TriangleSamplerBenchmark { * @param data Input data. */ private static void runSampler(Blackhole bh, SamplerData data) { - final Sampler sampler = data.getSampler(); + final ObjectSampler<double[]> sampler = data.getSampler(); for (int i = data.getSize() - 1; i >= 0; i--) { bh.consume(sampler.sample()); } diff --git a/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/sampling/shape/UnitBallSamplerBenchmark.java b/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/sampling/shape/UnitBallSamplerBenchmark.java index 8fc30ff..281b5f4 100644 --- a/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/sampling/shape/UnitBallSamplerBenchmark.java +++ b/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/sampling/shape/UnitBallSamplerBenchmark.java @@ -18,6 +18,7 @@ package org.apache.commons.rng.examples.jmh.sampling.shape; import org.apache.commons.rng.UniformRandomProvider; +import org.apache.commons.rng.sampling.ObjectSampler; import org.apache.commons.rng.sampling.distribution.ContinuousSampler; import org.apache.commons.rng.sampling.distribution.NormalizedGaussianSampler; import org.apache.commons.rng.sampling.distribution.ZigguratSampler; @@ -64,21 +65,9 @@ public class UnitBallSamplerBenchmark { private static final long LOWER_53_BITS = -1L >>> 11; /** - * The sampler. - */ - private interface Sampler { - /** - * Gets the next sample. - * - * @return the sample - */ - double[] sample(); - } - - /** * Base class for a sampler using a provided source of randomness. */ - private abstract static class BaseSampler implements Sampler { + private abstract static class BaseSampler implements ObjectSampler<double[]> { /** The source of randomness. */ protected UniformRandomProvider rng; @@ -100,7 +89,7 @@ public class UnitBallSamplerBenchmark { @State(Scope.Benchmark) public abstract static class SamplerData { /** The sampler. */ - private Sampler sampler; + private ObjectSampler<double[]> sampler; /** The number of samples. */ @Param({//"1", @@ -121,7 +110,7 @@ public class UnitBallSamplerBenchmark { * * @return the sampler */ - public Sampler getSampler() { + public ObjectSampler<double[]> getSampler() { return sampler; } @@ -141,7 +130,7 @@ public class UnitBallSamplerBenchmark { * @param rng the source of randomness * @return the sampler */ - protected abstract Sampler createSampler(UniformRandomProvider rng); + protected abstract ObjectSampler<double[]> createSampler(UniformRandomProvider rng); } /** @@ -164,7 +153,7 @@ public class UnitBallSamplerBenchmark { /** {@inheritDoc} */ @Override - protected Sampler createSampler(final UniformRandomProvider rng) { + protected ObjectSampler<double[]> createSampler(final UniformRandomProvider rng) { if (BASELINE.equals(type)) { return () -> new double[] {0.5}; } else if (SIGNED_DOUBLE.equals(type)) { @@ -196,7 +185,7 @@ public class UnitBallSamplerBenchmark { /** {@inheritDoc} */ @Override - protected Sampler createSampler(final UniformRandomProvider rng) { + protected ObjectSampler<double[]> createSampler(final UniformRandomProvider rng) { if (BASELINE.equals(type)) { return () -> new double[] {0.5, 0}; } else if (REJECTION.equals(type)) { @@ -341,7 +330,7 @@ public class UnitBallSamplerBenchmark { /** {@inheritDoc} */ @Override - protected Sampler createSampler(final UniformRandomProvider rng) { + protected ObjectSampler<double[]> createSampler(final UniformRandomProvider rng) { if (BASELINE.equals(type)) { return () -> new double[] {0.5, 0, 0}; } else if (REJECTION.equals(type)) { @@ -506,9 +495,9 @@ public class UnitBallSamplerBenchmark { /** {@inheritDoc} */ @Override - protected Sampler createSampler(final UniformRandomProvider rng) { + protected ObjectSampler<double[]> createSampler(final UniformRandomProvider rng) { if (BASELINE.equals(type)) { - return new Sampler() { + return new ObjectSampler<double[]>() { private final int dim = dimension; @Override public double[] sample() { @@ -745,7 +734,7 @@ public class UnitBallSamplerBenchmark { * @param data Input data. */ private static void runSampler(Blackhole bh, SamplerData data) { - final Sampler sampler = data.getSampler(); + final ObjectSampler<double[]> sampler = data.getSampler(); for (int i = data.getSize() - 1; i >= 0; i--) { bh.consume(sampler.sample()); }
