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.AhrensDieterExponentialSampler;
23 import org.apache.commons.rng.sampling.distribution.AhrensDieterMarsagliaTsangGammaSampler;
24 import org.apache.commons.rng.sampling.distribution.BoxMullerNormalizedGaussianSampler;
25 import org.apache.commons.rng.sampling.distribution.ChengBetaSampler;
26 import org.apache.commons.rng.sampling.distribution.ContinuousSampler;
27 import org.apache.commons.rng.sampling.distribution.ContinuousUniformSampler;
28 import org.apache.commons.rng.sampling.distribution.InverseTransformParetoSampler;
29 import org.apache.commons.rng.sampling.distribution.LevySampler;
30 import org.apache.commons.rng.sampling.distribution.LogNormalSampler;
31 import org.apache.commons.rng.sampling.distribution.MarsagliaNormalizedGaussianSampler;
32 import org.apache.commons.rng.sampling.distribution.StableSampler;
33 import org.apache.commons.rng.sampling.distribution.TSampler;
34 import org.apache.commons.rng.sampling.distribution.ZigguratSampler;
35 import org.apache.commons.rng.sampling.distribution.ZigguratNormalizedGaussianSampler;
36
37 import org.openjdk.jmh.annotations.Benchmark;
38 import org.openjdk.jmh.annotations.BenchmarkMode;
39 import org.openjdk.jmh.annotations.Fork;
40 import org.openjdk.jmh.annotations.Measurement;
41 import org.openjdk.jmh.annotations.Mode;
42 import org.openjdk.jmh.annotations.OutputTimeUnit;
43 import org.openjdk.jmh.annotations.Param;
44 import org.openjdk.jmh.annotations.Scope;
45 import org.openjdk.jmh.annotations.Setup;
46 import org.openjdk.jmh.annotations.State;
47 import org.openjdk.jmh.annotations.Warmup;
48
49 import java.util.concurrent.TimeUnit;
50
51
52
53
54
55 @BenchmarkMode(Mode.AverageTime)
56 @OutputTimeUnit(TimeUnit.NANOSECONDS)
57 @Warmup(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
58 @Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
59 @State(Scope.Benchmark)
60 @Fork(value = 1, jvmArgs = {"-server", "-Xms128M", "-Xmx128M"})
61 public class ContinuousSamplersPerformance {
62
63
64
65
66
67 private double value;
68
69
70
71
72
73
74 @State(Scope.Benchmark)
75 public static class Sources extends RandomSources {
76
77
78
79 @Param({"BoxMullerNormalizedGaussianSampler",
80 "MarsagliaNormalizedGaussianSampler",
81 "ZigguratNormalizedGaussianSampler",
82 "ZigguratSampler.NormalizedGaussian",
83 "AhrensDieterExponentialSampler",
84 "ZigguratSampler.Exponential",
85 "AhrensDieterGammaSampler",
86 "MarsagliaTsangGammaSampler",
87 "LevySampler",
88 "LogNormalBoxMullerNormalizedGaussianSampler",
89 "LogNormalMarsagliaNormalizedGaussianSampler",
90 "LogNormalZigguratNormalizedGaussianSampler",
91 "LogNormalSampler.ZigguratSampler.NormalizedGaussian",
92 "ChengBetaSampler",
93 "ContinuousUniformSampler",
94 "InverseTransformParetoSampler",
95 "StableSampler",
96 "TSampler"})
97 private String samplerType;
98
99
100 private ContinuousSampler sampler;
101
102
103
104
105 public ContinuousSampler getSampler() {
106 return sampler;
107 }
108
109
110 @Override
111 @Setup
112 public void setup() {
113 super.setup();
114 final UniformRandomProvider rng = getGenerator();
115 if ("BoxMullerNormalizedGaussianSampler".equals(samplerType)) {
116 sampler = BoxMullerNormalizedGaussianSampler.of(rng);
117 } else if ("MarsagliaNormalizedGaussianSampler".equals(samplerType)) {
118 sampler = MarsagliaNormalizedGaussianSampler.of(rng);
119 } else if ("ZigguratNormalizedGaussianSampler".equals(samplerType)) {
120 sampler = ZigguratNormalizedGaussianSampler.of(rng);
121 } else if ("ZigguratSampler.NormalizedGaussian".equals(samplerType)) {
122 sampler = ZigguratSampler.NormalizedGaussian.of(rng);
123 } else if ("AhrensDieterExponentialSampler".equals(samplerType)) {
124 sampler = AhrensDieterExponentialSampler.of(rng, 4.56);
125 } else if ("ZigguratSampler.Exponential".equals(samplerType)) {
126 sampler = ZigguratSampler.Exponential.of(rng, 4.56);
127 } else if ("AhrensDieterGammaSampler".equals(samplerType)) {
128
129 sampler = AhrensDieterMarsagliaTsangGammaSampler.of(rng, 0.76, 9.8);
130 } else if ("MarsagliaTsangGammaSampler".equals(samplerType)) {
131
132 sampler = AhrensDieterMarsagliaTsangGammaSampler.of(rng, 12.34, 9.8);
133 } else if ("LevySampler".equals(samplerType)) {
134 sampler = LevySampler.of(rng, 1.23, 4.56);
135 } else if ("LogNormalBoxMullerNormalizedGaussianSampler".equals(samplerType)) {
136 sampler = LogNormalSampler.of(BoxMullerNormalizedGaussianSampler.of(rng), 12.3, 4.6);
137 } else if ("LogNormalMarsagliaNormalizedGaussianSampler".equals(samplerType)) {
138 sampler = LogNormalSampler.of(MarsagliaNormalizedGaussianSampler.of(rng), 12.3, 4.6);
139 } else if ("LogNormalZigguratNormalizedGaussianSampler".equals(samplerType)) {
140 sampler = LogNormalSampler.of(ZigguratNormalizedGaussianSampler.of(rng), 12.3, 4.6);
141 } else if ("LogNormalSampler.ZigguratSampler.NormalizedGaussian".equals(samplerType)) {
142 sampler = LogNormalSampler.of(ZigguratSampler.NormalizedGaussian.of(rng), 12.3, 4.6);
143 } else if ("ChengBetaSampler".equals(samplerType)) {
144 sampler = ChengBetaSampler.of(rng, 0.45, 6.7);
145 } else if ("ContinuousUniformSampler".equals(samplerType)) {
146 sampler = ContinuousUniformSampler.of(rng, 123.4, 5678.9);
147 } else if ("InverseTransformParetoSampler".equals(samplerType)) {
148 sampler = InverseTransformParetoSampler.of(rng, 23.45, 0.1234);
149 } else if ("StableSampler".equals(samplerType)) {
150 sampler = StableSampler.of(rng, 1.3, 0.2);
151 } else if ("TSampler".equals(samplerType)) {
152 sampler = TSampler.of(rng, 1.23);
153 }
154 }
155 }
156
157
158
159
160
161
162
163
164 @Benchmark
165 public double baseline() {
166 return value;
167 }
168
169
170
171
172
173
174
175 @Benchmark
176 public double sample(Sources sources) {
177 return sources.getSampler().sample();
178 }
179 }