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 59ab70d300ca1221cb6512faf698be8c1c68b163 Author: aherbert <aherb...@apache.org> AuthorDate: Wed Feb 27 14:24:55 2019 +0000 ConstructionPerformance: added generation of numbers after construction. No RNG is constructed without using it so the benchmark now exercises the RNG. Also added a method to construct using a long seed. --- .../rng/examples/jmh/ConstructionPerformance.java | 83 ++++++++++++++++------ 1 file changed, 62 insertions(+), 21 deletions(-) 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 4c201f3..9576c39 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 @@ -37,6 +37,7 @@ import java.util.concurrent.TimeUnit; import org.apache.commons.rng.UniformRandomProvider; import org.apache.commons.rng.core.source32.ISAACRandom; +import org.apache.commons.rng.core.source32.IntProvider; import org.apache.commons.rng.core.source32.JDKRandom; import org.apache.commons.rng.core.source32.KISSRandom; import org.apache.commons.rng.core.source32.MersenneTwister; @@ -392,12 +393,39 @@ public class ConstructionPerformance { } /** + * Number of random values to generate after construction. + */ + @Param({"0", "1", "100", "10000"}) + private int numValues; + + /** + * Consume the generator and run the native generation method a set number of times. + * + * @param bh Data sink. + * @param rng the random generator + */ + private void runGenerator(Blackhole bh, UniformRandomProvider rng) { + bh.consume(rng); + // For fairness this should run when no values are generated + if (rng instanceof IntProvider) { + for (int i = numValues; i-- != 0; ) { + bh.consume(rng.nextInt()); + } + } else { + // Assume a long provider + for (int i = numValues; i-- != 0; ) { + bh.consume(rng.nextLong()); + } + } + } + + /** * @param bh Data sink. */ @Benchmark public void newJDKRandom(Blackhole bh) { for (int i = 0; i < SEEDS; i++) { - bh.consume(new JDKRandom(LONG_SEEDS[i])); + runGenerator(bh, new JDKRandom(LONG_SEEDS[i])); } } @@ -407,7 +435,7 @@ public class ConstructionPerformance { @Benchmark public void newWell512a(Blackhole bh) { for (int i = 0; i < SEEDS; i++) { - bh.consume(new Well512a(INT_ARRAY_SEEDS[i])); + runGenerator(bh, new Well512a(INT_ARRAY_SEEDS[i])); } } @@ -417,7 +445,7 @@ public class ConstructionPerformance { @Benchmark public void newWell1024a(Blackhole bh) { for (int i = 0; i < SEEDS; i++) { - bh.consume(new Well1024a(INT_ARRAY_SEEDS[i])); + runGenerator(bh, new Well1024a(INT_ARRAY_SEEDS[i])); } } @@ -427,7 +455,7 @@ public class ConstructionPerformance { @Benchmark public void newWell19937a(Blackhole bh) { for (int i = 0; i < SEEDS; i++) { - bh.consume(new Well19937a(INT_ARRAY_SEEDS[i])); + runGenerator(bh, new Well19937a(INT_ARRAY_SEEDS[i])); } } @@ -437,7 +465,7 @@ public class ConstructionPerformance { @Benchmark public void newWell19937c(Blackhole bh) { for (int i = 0; i < SEEDS; i++) { - bh.consume(new Well19937c(INT_ARRAY_SEEDS[i])); + runGenerator(bh, new Well19937c(INT_ARRAY_SEEDS[i])); } } @@ -447,7 +475,7 @@ public class ConstructionPerformance { @Benchmark public void newWell44497a(Blackhole bh) { for (int i = 0; i < SEEDS; i++) { - bh.consume(new Well44497a(INT_ARRAY_SEEDS[i])); + runGenerator(bh, new Well44497a(INT_ARRAY_SEEDS[i])); } } @@ -457,7 +485,7 @@ public class ConstructionPerformance { @Benchmark public void newWell44497b(Blackhole bh) { for (int i = 0; i < SEEDS; i++) { - bh.consume(new Well44497b(INT_ARRAY_SEEDS[i])); + runGenerator(bh, new Well44497b(INT_ARRAY_SEEDS[i])); } } @@ -467,7 +495,7 @@ public class ConstructionPerformance { @Benchmark public void newMersenneTwister(Blackhole bh) { for (int i = 0; i < SEEDS; i++) { - bh.consume(new MersenneTwister(INT_ARRAY_SEEDS[i])); + runGenerator(bh, new MersenneTwister(INT_ARRAY_SEEDS[i])); } } @@ -477,7 +505,7 @@ public class ConstructionPerformance { @Benchmark public void newISAACRandom(Blackhole bh) { for (int i = 0; i < SEEDS; i++) { - bh.consume(new ISAACRandom(INT_ARRAY_SEEDS[i])); + runGenerator(bh, new ISAACRandom(INT_ARRAY_SEEDS[i])); } } @@ -487,7 +515,7 @@ public class ConstructionPerformance { @Benchmark public void newSplitMix64(Blackhole bh) { for (int i = 0; i < SEEDS; i++) { - bh.consume(new SplitMix64(LONG_SEEDS[i])); + runGenerator(bh, new SplitMix64(LONG_SEEDS[i])); } } @@ -497,7 +525,7 @@ public class ConstructionPerformance { @Benchmark public void newXorShift1024Star(Blackhole bh) { for (int i = 0; i < SEEDS; i++) { - bh.consume(new XorShift1024Star(LONG_ARRAY_SEEDS[i])); + runGenerator(bh, new XorShift1024Star(LONG_ARRAY_SEEDS[i])); } } @@ -507,7 +535,7 @@ public class ConstructionPerformance { @Benchmark public void newTwoCmres(Blackhole bh) { for (int i = 0; i < SEEDS; i++) { - bh.consume(new TwoCmres(INTEGER_SEEDS[i])); + runGenerator(bh, new TwoCmres(INTEGER_SEEDS[i])); } } @@ -517,7 +545,7 @@ public class ConstructionPerformance { @Benchmark public void newMersenneTwister64(Blackhole bh) { for (int i = 0; i < SEEDS; i++) { - bh.consume(new MersenneTwister64(LONG_ARRAY_SEEDS[i])); + runGenerator(bh, new MersenneTwister64(LONG_ARRAY_SEEDS[i])); } } @@ -527,7 +555,7 @@ public class ConstructionPerformance { @Benchmark public void newMultiplyWithCarry256(Blackhole bh) { for (int i = 0; i < SEEDS; i++) { - bh.consume(new MultiplyWithCarry256(INT_ARRAY_SEEDS[i])); + runGenerator(bh, new MultiplyWithCarry256(INT_ARRAY_SEEDS[i])); } } @@ -537,7 +565,7 @@ public class ConstructionPerformance { @Benchmark public void newKISSRandom(Blackhole bh) { for (int i = 0; i < SEEDS; i++) { - bh.consume(new KISSRandom(INT_ARRAY_SEEDS[i])); + runGenerator(bh, new KISSRandom(INT_ARRAY_SEEDS[i])); } } @@ -553,7 +581,7 @@ public class ConstructionPerformance { final Object[] nativeSeeds = sources.getNativeSeeds(); final Constructor<?> constructor = sources.getConstructor(); for (int i = 0; i < SEEDS; i++) { - bh.consume(constructor.newInstance(nativeSeeds[i])); + runGenerator(bh, (UniformRandomProvider) constructor.newInstance(nativeSeeds[i])); } } @@ -569,7 +597,8 @@ public class ConstructionPerformance { final Object[] nativeSeeds = sources.getNativeSeeds(); final Class<?> implementingClass = sources.getImplementingClass(); for (int i = 0; i < SEEDS; i++) { - bh.consume(implementingClass.getConstructor(nativeSeeds[i].getClass()).newInstance(nativeSeeds[i])); + runGenerator(bh, (UniformRandomProvider) implementingClass.getConstructor( + nativeSeeds[i].getClass()).newInstance(nativeSeeds[i])); } } @@ -581,7 +610,7 @@ public class ConstructionPerformance { public void createNullSeed(Sources sources, Blackhole bh) { final RandomSource randomSource = sources.getRandomSource(); for (int i = 0; i < SEEDS; i++) { - bh.consume(RandomSource.create(randomSource, null)); + runGenerator(bh, RandomSource.create(randomSource, null)); } } @@ -594,7 +623,7 @@ public class ConstructionPerformance { final RandomSource randomSource = sources.getRandomSource(); final Object[] nativeSeeds = sources.getNativeSeeds(); for (int i = 0; i < SEEDS; i++) { - bh.consume(RandomSource.create(randomSource, nativeSeeds[i])); + runGenerator(bh, RandomSource.create(randomSource, nativeSeeds[i])); } } @@ -613,7 +642,19 @@ public class ConstructionPerformance { final RandomSource randomSource = sources.getRandomSource(); final Object[] nativeSeeds1 = sources.getNativeSeeds1(); for (int i = 0; i < SEEDS; i++) { - bh.consume(RandomSource.create(randomSource, nativeSeeds1[i])); + runGenerator(bh, RandomSource.create(randomSource, nativeSeeds1[i])); + } + } + + /** + * @param sources Source of randomness. + * @param bh Data sink. + */ + @Benchmark + public void createLongSeed(Sources sources, Blackhole bh) { + final RandomSource randomSource = sources.getRandomSource(); + for (int i = 0; i < SEEDS; i++) { + runGenerator(bh, RandomSource.create(randomSource, LONG_SEEDS[i])); } } @@ -626,7 +667,7 @@ public class ConstructionPerformance { final RandomSource randomSource = sources.getRandomSource(); final byte[][] byteSeeds = sources.getByteSeeds(); for (int i = 0; i < SEEDS; i++) { - bh.consume(RandomSource.create(randomSource, byteSeeds[i])); + runGenerator(bh, RandomSource.create(randomSource, byteSeeds[i])); } } }