View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.rng.examples.jmh.sampling.distribution;
19  
20  import org.apache.commons.rng.UniformRandomProvider;
21  import org.apache.commons.rng.examples.jmh.RandomSources;
22  import org.apache.commons.rng.sampling.distribution.BoxMullerNormalizedGaussianSampler;
23  import org.apache.commons.rng.sampling.distribution.MarsagliaNormalizedGaussianSampler;
24  import org.apache.commons.rng.sampling.distribution.NormalizedGaussianSampler;
25  import org.apache.commons.rng.sampling.distribution.ZigguratNormalizedGaussianSampler;
26  import org.apache.commons.rng.sampling.distribution.ZigguratSampler;
27  import org.openjdk.jmh.annotations.Benchmark;
28  import org.openjdk.jmh.annotations.BenchmarkMode;
29  import org.openjdk.jmh.annotations.Mode;
30  import org.openjdk.jmh.annotations.Warmup;
31  import org.openjdk.jmh.annotations.Measurement;
32  import org.openjdk.jmh.annotations.State;
33  import org.openjdk.jmh.annotations.Fork;
34  import org.openjdk.jmh.annotations.Scope;
35  import org.openjdk.jmh.annotations.Setup;
36  import org.openjdk.jmh.annotations.OutputTimeUnit;
37  import org.openjdk.jmh.annotations.Param;
38  import java.util.concurrent.TimeUnit;
39  import java.util.Random;
40  
41  /**
42   * Benchmark to compare the speed of generation of normally-distributed random
43   * numbers of {@link Random#nextGaussian()} against implementations of
44   * {@link NormalizedGaussianSampler}.
45   */
46  @BenchmarkMode(Mode.AverageTime)
47  @OutputTimeUnit(TimeUnit.NANOSECONDS)
48  @Warmup(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
49  @Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
50  @State(Scope.Benchmark)
51  @Fork(value = 1, jvmArgs = {"-server", "-Xms128M", "-Xmx128M"})
52  public class NextGaussianPerformance {
53      /**
54       * The value.
55       *
56       * <p>This must NOT be final!</p>
57       */
58      private double value;
59  
60      /**
61       * The {@link Random} to use for testing.
62       * This uses a non-final instance created for the benchmark to make a fair comparison
63       * to the other Gaussian samplers by using the same setup and sampler access.
64       */
65      @State(Scope.Benchmark)
66      public static class JDKSource {
67          /** JDK's generator. */
68          private Random random;
69  
70          /**
71           * @return the sampler.
72           */
73          public Random getSampler() {
74              return random;
75          }
76  
77          /** Instantiates sampler. */
78          @Setup
79          public void setup() {
80              random = new Random();
81          }
82      }
83  
84      /**
85       * The {@link NormalizedGaussianSampler} samplers to use for testing. Creates the sampler for each
86       * {@link org.apache.commons.rng.simple.RandomSource RandomSource} in the default
87       * {@link RandomSources}.
88       */
89      @State(Scope.Benchmark)
90      public static class Sources extends RandomSources {
91          /** The sampler type. */
92          @Param({"BoxMullerNormalizedGaussianSampler",
93                  "MarsagliaNormalizedGaussianSampler",
94                  "ZigguratNormalizedGaussianSampler",
95                  "ZigguratSampler.NormalizedGaussian"})
96          private String samplerType;
97  
98          /** The sampler. */
99          private NormalizedGaussianSampler sampler;
100 
101         /**
102          * @return the sampler.
103          */
104         public NormalizedGaussianSampler getSampler() {
105             return sampler;
106         }
107 
108         /** Instantiates sampler. */
109         @Override
110         @Setup
111         public void setup() {
112             super.setup();
113             final UniformRandomProvider rng = getGenerator();
114             if ("BoxMullerNormalizedGaussianSampler".equals(samplerType)) {
115                 sampler = BoxMullerNormalizedGaussianSampler.of(rng);
116             } else if ("MarsagliaNormalizedGaussianSampler".equals(samplerType)) {
117                 sampler = MarsagliaNormalizedGaussianSampler.of(rng);
118             } else if ("ZigguratNormalizedGaussianSampler".equals(samplerType)) {
119                 sampler = ZigguratNormalizedGaussianSampler.of(rng);
120             } else if ("ZigguratSampler.NormalizedGaussian".equals(samplerType)) {
121                 sampler = ZigguratSampler.NormalizedGaussian.of(rng);
122             } else {
123                 throw new IllegalStateException("Unknown sampler type: " + samplerType);
124             }
125         }
126     }
127 
128     /**
129      * Baseline for the JMH timing overhead for production of an {@code double} value.
130      *
131      * @return the {@code double} value
132      */
133     @Benchmark
134     public double baseline() {
135         return value;
136     }
137 
138     /**
139      * Run JDK {@link Random} gaussian sampler.
140      *
141      * @param source Source of randomness.
142      * @return the sample value
143      */
144     @Benchmark
145     public double sampleJDK(JDKSource source) {
146         return source.getSampler().nextGaussian();
147     }
148 
149     /**
150      * Run the {@link NormalizedGaussianSampler} sampler.
151      *
152      * @param sources Source of randomness.
153      * @return the sample value
154      */
155     @Benchmark
156     public double sample(Sources sources) {
157         return sources.getSampler().sample();
158     }
159 }