1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
43
44
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
55
56
57
58 private double value;
59
60
61
62
63
64
65 @State(Scope.Benchmark)
66 public static class JDKSource {
67
68 private Random random;
69
70
71
72
73 public Random getSampler() {
74 return random;
75 }
76
77
78 @Setup
79 public void setup() {
80 random = new Random();
81 }
82 }
83
84
85
86
87
88
89 @State(Scope.Benchmark)
90 public static class Sources extends RandomSources {
91
92 @Param({"BoxMullerNormalizedGaussianSampler",
93 "MarsagliaNormalizedGaussianSampler",
94 "ZigguratNormalizedGaussianSampler",
95 "ZigguratSampler.NormalizedGaussian"})
96 private String samplerType;
97
98
99 private NormalizedGaussianSampler sampler;
100
101
102
103
104 public NormalizedGaussianSampler getSampler() {
105 return sampler;
106 }
107
108
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
130
131
132
133 @Benchmark
134 public double baseline() {
135 return value;
136 }
137
138
139
140
141
142
143
144 @Benchmark
145 public double sampleJDK(JDKSource source) {
146 return source.getSampler().nextGaussian();
147 }
148
149
150
151
152
153
154
155 @Benchmark
156 public double sample(Sources sources) {
157 return sources.getSampler().sample();
158 }
159 }