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.RestorableUniformRandomProvider;
21 import org.apache.commons.rng.UniformRandomProvider;
22 import org.apache.commons.rng.sampling.distribution.DiscreteUniformSampler;
23 import org.apache.commons.rng.sampling.distribution.SharedStateDiscreteSampler;
24 import org.apache.commons.rng.simple.RandomSource;
25 import org.openjdk.jmh.annotations.Benchmark;
26 import org.openjdk.jmh.annotations.BenchmarkMode;
27 import org.openjdk.jmh.annotations.Mode;
28 import org.openjdk.jmh.annotations.Warmup;
29 import org.openjdk.jmh.infra.Blackhole;
30 import org.openjdk.jmh.annotations.Measurement;
31 import org.openjdk.jmh.annotations.State;
32 import org.openjdk.jmh.annotations.Fork;
33 import org.openjdk.jmh.annotations.Scope;
34 import org.openjdk.jmh.annotations.Setup;
35 import org.openjdk.jmh.annotations.OutputTimeUnit;
36 import org.openjdk.jmh.annotations.Param;
37
38 import java.util.concurrent.TimeUnit;
39
40
41
42
43
44 @BenchmarkMode(Mode.AverageTime)
45 @OutputTimeUnit(TimeUnit.NANOSECONDS)
46 @Warmup(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
47 @Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
48 @State(Scope.Benchmark)
49 @Fork(value = 1, jvmArgs = { "-server", "-Xms128M", "-Xmx128M" })
50 public class DiscreteUniformSamplerGenerationPerformance {
51
52 @Param({
53 "1",
54 "2",
55 "4",
56 "8",
57 "16",
58 "1000000",
59 })
60 private int samples;
61
62
63
64
65 @State(Scope.Benchmark)
66 public static class Sources {
67
68
69
70
71
72
73
74
75 @Param({"SPLIT_MIX_64",
76
77
78
79 })
80 private String randomSourceName;
81
82
83 private RestorableUniformRandomProvider generator;
84
85
86
87
88 public UniformRandomProvider getGenerator() {
89 return generator;
90 }
91
92
93 @Setup
94 public void setup() {
95 final RandomSource randomSource = RandomSource.valueOf(randomSourceName);
96 generator = randomSource.create();
97 }
98 }
99
100
101
102
103 @State(Scope.Benchmark)
104 public static class IntRange {
105
106
107
108
109
110
111
112
113
114
115
116 @Param({
117 "256",
118 "257",
119 "1073741825",
120 })
121 private int upperBound;
122
123
124
125
126
127
128 public int getUpperBound() {
129 return upperBound;
130 }
131 }
132
133
134
135
136
137
138
139
140
141
142
143
144 @Benchmark
145 public void nextIntBaseline(Blackhole bh, Sources source) {
146 int sum = 0;
147 for (int i = 0; i < samples; i++) {
148 sum += source.getGenerator().nextInt();
149 }
150 bh.consume(sum);
151 }
152
153
154
155
156
157
158 @Benchmark
159 public void nextIntRange(Blackhole bh, Sources source, IntRange range) {
160 final int n = range.getUpperBound();
161 int sum = 0;
162 for (int i = 0; i < samples; i++) {
163 sum += source.getGenerator().nextInt(n);
164 }
165 bh.consume(sum);
166 }
167
168
169
170
171
172
173 @Benchmark
174 public void nextDiscreteUniformSampler(Blackhole bh, Sources source, IntRange range) {
175
176 final SharedStateDiscreteSampler sampler = DiscreteUniformSampler.of(
177 source.getGenerator(), 0, range.getUpperBound() - 1);
178 int sum = 0;
179 for (int i = 0; i < samples; i++) {
180 sum += sampler.sample();
181 }
182 bh.consume(sum);
183 }
184 }