24
24
25
25
import org .openjdk .jmh .annotations .Benchmark ;
26
26
import org .openjdk .jmh .annotations .BenchmarkMode ;
27
+ import org .openjdk .jmh .annotations .Fork ;
28
+ import org .openjdk .jmh .annotations .Measurement ;
27
29
import org .openjdk .jmh .annotations .Mode ;
28
30
import org .openjdk .jmh .annotations .OperationsPerInvocation ;
29
31
import org .openjdk .jmh .annotations .OutputTimeUnit ;
30
32
import org .openjdk .jmh .annotations .Scope ;
31
33
import org .openjdk .jmh .annotations .Setup ;
32
34
import org .openjdk .jmh .annotations .State ;
33
35
import org .openjdk .jmh .annotations .Param ;
36
+ import org .openjdk .jmh .annotations .Warmup ;
34
37
import org .openjdk .jmh .infra .Blackhole ;
35
38
36
39
import java .math .BigInteger ;
40
43
@ BenchmarkMode (Mode .AverageTime )
41
44
@ OutputTimeUnit (TimeUnit .NANOSECONDS )
42
45
@ State (Scope .Thread )
46
+ @ Warmup (iterations = 5 , time = 1 )
47
+ @ Measurement (iterations = 5 , time = 1 )
48
+ @ Fork (value = 3 )
43
49
public class BigIntegers {
44
50
45
- private BigInteger [] hugeArray , largeArray , smallArray , shiftArray , smallShiftArray ;
51
+ private BigInteger [] hugeArray , largeArray , smallArray , shiftArray ;
46
52
public String [] dummyStringArray ;
47
53
public Object [] dummyArr ;
48
54
private static final int TESTSIZE = 1000 ;
49
55
50
- @ Param ({"32" , "64" , "96" , "128" , "160" , "192" , "224" , "256" })
51
- private int maxNumbits ;
52
-
53
56
@ Setup
54
57
public void setup () {
55
58
Random r = new Random (1123 );
@@ -72,9 +75,6 @@ public void setup() {
72
75
* Each array entry is atmost 16k bits
73
76
* in size
74
77
*/
75
- smallShiftArray = new BigInteger [TESTSIZE ]; /*
76
- * Small numbers, bits count in range [maxNumbits - 31, maxNumbits]
77
- */
78
78
79
79
dummyStringArray = new String [TESTSIZE ];
80
80
dummyArr = new Object [TESTSIZE ];
@@ -87,7 +87,6 @@ public void setup() {
87
87
largeArray [i ] = new BigInteger ("" + ((long ) value + (long ) Integer .MAX_VALUE ));
88
88
smallArray [i ] = new BigInteger ("" + ((long ) value / 1000 ));
89
89
shiftArray [i ] = new BigInteger (numbits , r );
90
- smallShiftArray [i ] = new BigInteger (Math .max (maxNumbits - value % 32 , 0 ), r );
91
90
}
92
91
}
93
92
@@ -182,32 +181,6 @@ public void testRightShift(Blackhole bh) {
182
181
bh .consume (tmp );
183
182
}
184
183
185
- /** Invokes the shiftLeft method of small BigInteger with different values. */
186
- @ Benchmark
187
- @ OperationsPerInvocation (TESTSIZE )
188
- public void testSmallLeftShift (Blackhole bh ) {
189
- Random rand = new Random ();
190
- int shift = rand .nextInt (30 ) + 1 ;
191
- BigInteger tmp = null ;
192
- for (BigInteger s : smallShiftArray ) {
193
- tmp = s .shiftLeft (shift );
194
- bh .consume (tmp );
195
- }
196
- }
197
-
198
- /** Invokes the shiftRight method of small BigInteger with different values. */
199
- @ Benchmark
200
- @ OperationsPerInvocation (TESTSIZE )
201
- public void testSmallRightShift (Blackhole bh ) {
202
- Random rand = new Random ();
203
- int shift = rand .nextInt (30 ) + 1 ;
204
- BigInteger tmp = null ;
205
- for (BigInteger s : smallShiftArray ) {
206
- tmp = s .shiftRight (shift );
207
- bh .consume (tmp );
208
- }
209
- }
210
-
211
184
/** Invokes the gcd method of BigInteger with different values. */
212
185
@ Benchmark
213
186
@ OperationsPerInvocation (TESTSIZE )
@@ -218,4 +191,52 @@ public void testGcd(Blackhole bh) {
218
191
bh .consume (i2 .gcd (i1 ));
219
192
}
220
193
}
194
+
195
+ @ BenchmarkMode (Mode .AverageTime )
196
+ @ OutputTimeUnit (TimeUnit .NANOSECONDS )
197
+ @ State (Scope .Thread )
198
+ @ Warmup (iterations = 5 , time = 1 )
199
+ @ Measurement (iterations = 5 , time = 1 )
200
+ @ Fork (value = 3 )
201
+ public static class SmallShifts {
202
+
203
+ @ Param ({"32" , "128" , "256" })
204
+ private int maxNumbits ;
205
+
206
+ /*
207
+ * Small numbers, bits count in range [maxNumbits - 31, maxNumbits]
208
+ */
209
+ BigInteger [] smallShiftArray = new BigInteger [TESTSIZE ];
210
+
211
+ @ Setup
212
+ public void setup () {
213
+ Random r = new Random (1123 );
214
+ for (int i = 0 ; i < TESTSIZE ; i ++) {
215
+ int value = Math .abs (r .nextInt ());
216
+ smallShiftArray [i ] = new BigInteger (Math .max (maxNumbits - value % 32 , 0 ), r );
217
+ }
218
+ }
219
+
220
+ /** Invokes the shiftLeft method of small BigInteger with different values. */
221
+ @ Benchmark
222
+ @ OperationsPerInvocation (TESTSIZE )
223
+ public void testLeftShift (Blackhole bh ) {
224
+ Random rand = new Random ();
225
+ int shift = rand .nextInt (30 ) + 1 ;
226
+ for (BigInteger s : smallShiftArray ) {
227
+ bh .consume (s .shiftLeft (shift ));
228
+ }
229
+ }
230
+
231
+ /** Invokes the shiftRight method of small BigInteger with different values. */
232
+ @ Benchmark
233
+ @ OperationsPerInvocation (TESTSIZE )
234
+ public void testRightShift (Blackhole bh ) {
235
+ Random rand = new Random ();
236
+ int shift = rand .nextInt (30 ) + 1 ;
237
+ for (BigInteger s : smallShiftArray ) {
238
+ bh .consume (s .shiftRight (shift ));
239
+ }
240
+ }
241
+ }
221
242
}
0 commit comments